如何在浏览器上显示GET消息的回复?

时间:2014-04-27 07:07:33

标签: python proxy http-caching

我获得了编写服务器程序的任务,更像是代理服务器,我在运行代理服务器时从浏览器发送GET请求。它应该在缓存中检查它是否存在然后发回回复(html文档),否则它会向服务器询问所请求的页面并将其返回到代理服务器,在那里它将被定向回客户端(浏览器)。 / p>

我做了大量的谷歌搜索以找到提示,但没有找到一个。如果有人能指出我正确的方向或给出一些暗示,我将不胜感激。

以下是我找到的一个示例代码,但它无法获取页面并在浏览器中显示。

# import the required library
import socket, threading

class ClientThread(threading.Thread):

    def __init__(self,ip,port, socket):
        threading.Thread.__init__(self)
        self.ip = ip
        self.port = port
        self.socket = socket
        print "[+] New thread started for "+ip+":"+str(port)

    # Entry point for thread
    def run(self):    
        print "Connection from : "+ip+":"+str(port)

        #clientsock.send("\nWelcome to the server\n\n")
        self.socket.send("\nWelcome to the server\n\n")
        data = "dummydata"

        while len(data):
            data = self.socket.recv(2048)   #clientsock.recv(2048)
            print "Client sent : "+data
            self.socket.send("You sent me : "+data)

        print "Client disconnected..."

host = "localhost"
port = 9999

# Create server socket object
tcpsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Port reused immediately after the socket is closed
tcpsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

# Bind the server socket to the port
tcpsock.bind((host,port))
threads = []


while True:
    # Start listening to new connections
    tcpsock.listen(4)
    print "\nListening for incoming connections..."
    (clientsock, (ip, port)) = tcpsock.accept()

    # Create new thread
    newthread = ClientThread(ip, port, clientsock)

    # Start new thread
    newthread.start()

    threads.append(newthread)

# Wait for threads to terminate
for t in threads:
    t.join()

我对python很新,我真的需要帮助。

0 个答案:

没有答案