我有一個家庭作業,包括在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訪問谷歌,我打招呼與錯誤頁面說err_empty_response。
通過與調試代碼加強使我意識到它的失敗在這條線
c.connect((hostn, 80))
,我不知道爲什麼。任何幫助將不勝感激。
P.S.我正在使用谷歌瀏覽器,Python 2.7和Windows 10對此進行測試。
切斷'www.'是危險的。沒有'www.''的名字也不需要解析,也不需要解析到具有'www.''的IP地址。 – jsfan
是的,這是有道理的。不幸的是,刪除部分我沒有修復問題 –
編號請參閱我的答案。您需要先解析名稱。檢查[getaddrinfo()']文檔(https://docs.python.org/2/library/socket.html#socket.getaddrinfo) – jsfan