2017-11-01 4 views
0

このコードを使用してHTTPプロキシキャッシュサーバーを作成しようとしています。私がコードを実行すると、実行されてポートに接続されますが、ブラウザから接続しようとすると、localhost:52523/www.google.comと入力すれば55555のポートが開きます他のサイト、特にHTTPを試してみると、localhost:52523/www.microcenter.comまたはlocalhost:52523/google.comと表示されますが、localhostはデータを送信しませんでした。 ERR_EMPTY_RESPONSEで、コンピュータにキャッシュファイルを作成してもコンソールに例外が表示されます。httpプロキシサーバーはhttpsサイトでのみ動作します

コードを編集して、プロキシサーバーを使用せずにブラウザで通常と同じように任意のWebサイトにアクセスできるようにする方法を知りたいと思います。あなたのエラーはURIスキームに関連していない

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://」を取り除くことから始めなければならない、あなたのプロキシリクエストに、プロトコルはGETまたはPOSTメソッドの一部ではありません –

答えて

0

(HTTPまたはHTTPS)が、ファイルやソケットの使用にwww.microcenter.comで作業することができるはずです。

あなたがファイルを開こうとしているとき:

file = open(file_to_use[1:], "r") 

あなたは(あなたの例ではhttp://ebay.com/)違法なファイルパスを渡しています。

あなたがたURIで作業しているとして、あなたは、スキーマをよりよく扱うことができるので、あなたは、urlparseのようなパーサを使用することができ、ホスト名、等...

例は:

url = urlparse(Req_path) 
file_to_use = url.hostname 
file = open(file_to_use, "r") 

のみ使用ファイル名としてのホスト名

もう1つの問題は、ソケットを使用することです。機能connectはあなたがやっていることであるスキーマを持つホスト名ではなく、ホスト名を受け取るべきです。ここでも、パーサの助けを借りて:そのほかに

serv_proxy.connect((url.hostname, 80)) 

、あなたは(examplesを参照)、クライアント上でlistenを呼び出すことはありませんので、あなたは、その行を削除することができます。

最後に、再びホスト名を使用して、新しいファイルを作成するには:

tmpFile = open(file_to_use, "wb") 
+0

httpアクセスの問題については –

+0

更新された答えを確認してください。 –

+0

私はコードを更新しましたので、違法なリクエストを示す例外に直面することはなくなりましたが、今からこのエラー接続を受け取りました:( '127.0.0.1'、61055) リクエストはURLに取得します:ebay.com トレースバック(最新の呼び出しの最後): ファイル "server.py"、ライン81、中 caching_object(splitMessage、Cli_Sock) ファイル "server.py"、ライン24、caching_objectで URL = urlparse(Req_path) はTypeError: 'module'オブジェクトは呼び出し可能ではありません –

関連する問題