【发布时间】:2016-06-13 09:27:41
【问题描述】:
我有一个家庭作业,其中涉及在 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))
我不知道为什么。任何帮助将不胜感激。
附:我正在使用 Google Chrome、Python 2.7 和 Windows 10 进行测试
【问题讨论】:
-
切断
www.是危险的。没有www.的名称也不必解析,也不必解析为与www.相同的IP 地址。 -
是的,这是有道理的。不幸的是,删除我删除的部分并不能解决问题
-
没有。看我的回答。您需要先解析名称。检查documentation for
getaddrinfo()
标签: python caching networking tcp proxy