http代理服务器仅适用于https网站

时间:2017-11-01 19:35:04

标签: python http proxy server

我正在尝试使用此代码来创建HTTP代理缓存服务器。当我运行代码时,它开始运行并连接到端口和所有东西,但是当我尝试从浏览器连接时,例如,如果我输入localhost,它会在55555上打开一个端口:52523 / www.google.com它工作正常但当我尝试其他网站专门的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)

1 个答案:

答案 0 :(得分:0)

您的错误与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(请参阅examples),因此您可以删除该行。

最后,再次创建新文件,使用主机名:

tmpFile = open(file_to_use, "wb")