http代理缓存服务器不限制浏览器功能

2024-09-26 18:00:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用这段代码来创建一个HTTP代理缓存服务器。当我运行代码时,它开始运行并连接到端口和所有东西,但当我尝试从浏览器连接时,例如,如果我输入localhost:52523/www.google.com,它会在55555上打开一个端口,但当我尝试其他站点(特别是HTTP)时,例如,localhost:52523/www.microcenter.com或只是localhost:52523/google.com它将显示localhost没有发送任何数据。在

ERR_EMPTY_响应并在控制台中显示异常,尽管它在我的计算机上创建了缓存文件。在

我想知道如何编辑代码,以便我可以访问任何网站,就像我通常在浏览器上不使用代理服务器一样。它应该能够与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)

Tags: thetopathinfromdatacliport
1条回答
网友
1楼 · 发布于 2024-09-26 18:00:59

代码中有一些错误:-

{1{1>不希望将查询^+1>作为请求的第一部分传递给{1>。在

应该添加一个额外的HOST头,它指定您正在使用的主机(即www.google.com)有些web服务器可能设置为忽略这一点,而是向您发送一个默认页面,但结果是断断续续的。在

您应该看看HTTP RFC,它给出了一些可以通过HTTP传递的其他头。在

您还可以安装类似FiddlerWireshark之类的东西,并监视一些示例HTTP调用,并查看有效负载的外观。在

相关问题 更多 >

    热门问题