簡體   English   中英

HTTP代理服務器僅適用於https站點

[英]http proxy server only working for https sites

我正在嘗試使用此代碼創建HTTP代理緩存服務器。 當我運行代碼時,它開始運行並連接到端口和其他所有端口,但是例如,當我嘗試通過瀏覽器進行連接時,如果我輸入localhost:52523 / www.google.com,它將在55555上打開端口,可以正常工作但是當我嘗試其他特定於HTTP的站點時,例如localhost:52523 / www.microcenter.com或僅localhost:52523 / google.com,它將顯示localhost沒有發送任何數據。 ERR_EMPTY_RESPONSE並在控制台中顯示一個異常,盡管它在我的計算機上創建了緩存文件。

我想了解如何編輯代碼,以便可以像在瀏覽器中一樣正常訪問任何網站,而無需使用代理服務器。 它應該能夠與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)

您的錯誤與URI方案(http或https)無關,而與文件和套接字的使用有關。

當您嘗試通過以下方式打開文件時:

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")

並僅使用主機名作為文件名。

另一個問題是使用套接字。 函數connect應該接收主機名,而不是您正在執行的具有模式的主機名。 再次,在解析器的幫助下:

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

除此之外,您不會在客戶端上調用listen (請參見示例 ),因此您可以刪除該行。

最后,再次使用主機名創建新文件:

tmpFile = open(file_to_use, "wb")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM