2017-11-03 1 views
0

このコードを使用してHTTPプロキシキャッシュサーバーを作成しようとしています。私はコードを実行すると、起動し、ポートとすべてに接続しますが、例えばブラウザから接続しようとすると、localhost:52523/www.google.comと入力すれば55555のポートが開きますが、他のサイト、特にHTTPたとえば、localhost:52523/www.microcenter.comまたはちょうどlocalhost:52523/google.comと表示されます。これは、localhostがデータを送信しなかったことを示します。HTTPプロキシキャッシュサーバーはブラウザ機能を制限していません

ERR_EMPTY_RESPONSEで、コンピュータにキャッシュファイルを作成してもコンソールに例外が表示されます。

コードを編集して、プロキシサーバーを使用せずにブラウザで通常と同じように任意のWebサイトにアクセスできるようにする方法を知りたいと思います。コード内のいくつかのエラーがありwww.microcenter.com

import socket 
import sys 
import urllib 
from urlparse import urlparse 
Serv_Sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # socket.socket 
function creates a socket. 
port = Serv_Sock.getsockname()[1] 
# Server socket created, bound and starting to listen 
Serv_Sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # socket.socket 
function creates a socket. 
Serv_Sock.bind(('',port)) 
Serv_Sock.listen(5) 
port = Serv_Sock.getsockname()[1] 
# Prepare a server socket 
print ("starting server on port %s...,"%(port)) 



def caching_object(splitMessage, Cli_Sock): 
    #this method is responsible for caching 
    Req_Type = splitMessage[0] 
    Req_path = splitMessage[1] 
    Req_path = Req_path[1:] 
    print "Request is ", Req_Type, " to URL : ", Req_path 

    #Searching available cache if file exists 
    url = urlparse(Req_path) 
    file_to_use = "/" + Req_path 
    print file_to_use 
    try: 
     file = open(file_to_use[5:], "r") 
     data = file.readlines() 
     print "File Present in Cache\n" 

     #Proxy Server Will Send A Response Message 
     #Cli_Sock.send("HTTP/1.0 200 OK\r\n") 
     #Cli_Sock.send("Content-Type:text/html") 
     #Cli_Sock.send("\r\n") 

     #Proxy Server Will Send Data 
     for i in range(0, len(data)): 
      print (data[i]) 
      Cli_Sock.send(data[i]) 
     print "Reading file from cache\n" 

    except IOError: 
     print "File Doesn't Exists In Cache\n fetching file from server \n 
creating cache" 
     serv_proxy = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
     host_name = Req_path 
     print "HOST NAME:", host_name 
     try: 
      serv_proxy.connect((url.host_name, 80)) 
      print 'Socket connected to port 80 of the host' 
      fileobj = serv_proxy.makefile('r', 0) 
      fileobj.write("GET " + "http://" + Req_path + " HTTP/1.0\n\n") 

      # Read the response into buffer 
      buffer = fileobj.readlines() 

      # Create a new file in the cache for the requested file. 
      # Also send the response in the buffer to client socket 
      # and the corresponding file in the cache 
      tmpFile = open(file_to_use, "wb") 
      for data in buffer: 
         tmpFile.write(data) 
         tcpCliSock.send(data) 
     except: 
      print 'Illegal Request' 

    Cli_Sock.close() 
while True: 
    # Start receiving data from the client 
    print 'Initiating server... \n Accepting connection\n' 
    Cli_Sock, addr = Serv_Sock.accept() # Accept a connection from client 
    #print addr 

    print ' connection received from: ', addr 
    message = Cli_Sock.recv(1024) #Recieves data from Socket 

    splitMessage = message.split() 
    if len(splitMessage) <= 1: 
     continue 

    caching_object(splitMessage, Cli_Sock) 
+0

[httpプロキシサーバーはhttpsサイトでのみ動作します](https://stackoverflow.com/questions/47062396/http-proxy-server-only-working-for-https-sites)の可能な複製 –

答えて

0

で作業することができるはず -

最初はGET要求はプロトコルが呼び出しの一部として渡されることを期待していないということですまた、ホストを期待するのではなく、代わりにGETをパス+クエリ文字列に限定する必要があります。

使用するホスト(www.google.comなど)を指定する追加のHOSTヘッダーを追加する必要があります。一部のウェブサーバーでは、これを無視してデフォルトのページを送信しますが、結果は断続的です。

HTTPで渡すことができるいくつかの他のヘッダーを提供するHTTP RFCにピークがあります。

FiddlerまたはWiresharkのようなものをインストールし、いくつかのサンプル番号HTTPコールを監視して、ペイロードがどのように見えるのかを確認することもできます。

関連する問題