繁体   English   中英

Python中的缓存代理服务器

[英]Cache Proxy Server in Python

我有一项家庭作业,涉及在Python中实现代理缓存服务器。 这个想法是在本地计算机上写我访问的临时文件的网页,然后在存储请求时根据请求访问它们。 现在,代码如下所示:

from socket import *
import sys

def main():
    #Create a server socket, bind it to a port and start listening
    tcpSerSock = socket(AF_INET, SOCK_STREAM) #Initializing socket
    tcpSerSock.bind(("", 8030)) #Binding socket to port
    tcpSerSock.listen(5) #Listening for page requests
    while True:
        #Start receiving data from the client
        print 'Ready to serve...'
        tcpCliSock, addr = tcpSerSock.accept()
        print 'Received a connection from:', addr
        message = tcpCliSock.recv(1024)
        print message

        #Extract the filename from the given message
        print message.split()[1]
        filename = message.split()[1].partition("/")[2]
        print filename
        fileExist = "false"
        filetouse = "/" + filename
        print filetouse

        try: #Check whether the file exists in the cache
            f = open(filetouse[1:], "r")
            outputdata = f.readlines()
            fileExist = "true"
            #ProxyServer finds a cache hit and generates a response message
            tcpCliSock.send("HTTP/1.0 200 OK\r\n")
            tcpCliSock.send("Content-Type:text/html\r\n")
            for data in outputdata:
                tcpCliSock.send(data)
            print 'Read from cache'
        except IOError: #Error handling for file not found in cache
            if fileExist == "false":

                c = socket(AF_INET, SOCK_STREAM) #Create a socket on the proxyserver
                hostn = filename.replace("www.","",1) 
                print hostn
                try:
                    c.connect((hostn, 80)) #https://docs.python.org/2/library/socket.html
                    # Create a temporary file on this socket and ask port 80 for
                    # the file requested by the client
                    fileobj = c.makefile('r', 0)
                    fileobj.write("GET " + "http://" + filename + "HTTP/1.0\r\n")
                    # Read the response into buffer
                    buffr = 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(filename,"wb")
                    for data in buffr:
                        tmpFile.write(data)
                        tcpCliSock.send(data)
                except:
                    print "Illegal request"
            else: #File not found
                print "404: File Not Found"
        tcpCliSock.close() #Close the client and the server sockets

main()

为了测试我的代码,我在本地主机上运行代理缓存,并相应地设置浏览器代理设置,如下所示

在此输入图像描述

但是,当我运行此代码并尝试使用Chrome访问Google时,我在错误页面上说err_empty_response。

用调试器单步执行代码使我意识到它在此行上失败

c.connect((hostn, 80))

而且我不知道为什么。 任何帮助将不胜感激。

PS我正在使用Google Chrome,Python 2.7和Windows 10对此进行测试

您不能在连接上使用名称。 Connect需要一个IP地址来连接。

您可以使用getaddrinfo()获得建立连接所需的套接字信息。 在我的pure-python-whois软件包中,我使用以下代码创建了一个连接:

def _openconn(self, server, timeout, port=None):
    port = port if port else 'nicname'
    try:
        for srv in socket.getaddrinfo(server, port, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_ADDRCONFIG):
            af, socktype, proto, _, sa = srv
            try:
                c = socket.socket(af, socktype, proto)
            except socket.error:
                c = None
                continue
            try:
                if self.source_addr:
                    c.bind(self.source_addr)
                c.settimeout(timeout)
                c.connect(sa)
            except socket.error:
                c.close()
                c = None
                continue
            break
    except socket.gaierror:
        return False

    return c

请注意,这不是很好的代码,因为循环实际上不存在任何替代使用其他方法的地方。 建立连接后,才应该中断循环。 但是,这应该作为使用getaddrinfo()

编辑:您也没有正确清除主机名。 当我尝试访问http://www.example.com/ ,显示/www.example.com/ ,但显然无法解决。 我建议您使用正则表达式来获取缓存的文件名。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM