Python 如何在浏览器上显示GET消息的回复?
我得到的任务是,我需要编写一个服务器程序,更像是一个代理服务器,在运行代理服务器时,我从浏览器发送GET请求。它应该在缓存中检查它是否存在,然后发回回复(html文档),否则它会询问服务器请求的页面,并将其返回到代理服务器,在那里它将被定向回客户端(浏览器) 我在谷歌上搜索了很多提示,但都没有找到。如果有人能为我指出正确的方向或给我一些提示,我将不胜感激 下面是我找到的一个示例代码,但它无法获取页面并在浏览器中显示Python 如何在浏览器上显示GET消息的回复?,python,proxy,http-caching,Python,Proxy,Http Caching,我得到的任务是,我需要编写一个服务器程序,更像是一个代理服务器,在运行代理服务器时,我从浏览器发送GET请求。它应该在缓存中检查它是否存在,然后发回回复(html文档),否则它会询问服务器请求的页面,并将其返回到代理服务器,在那里它将被定向回客户端(浏览器) 我在谷歌上搜索了很多提示,但都没有找到。如果有人能为我指出正确的方向或给我一些提示,我将不胜感激 下面是我找到的一个示例代码,但它无法获取页面并在浏览器中显示 # import the required library import soc
# 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非常陌生,在这里我真的需要帮助