Я создал прокси-сервер, который получает запросы, ищет запрашиваемый файл в кеше. Если доступно, он возвращает кешированный файл. Если файл недоступен, он запрашивает фактический сервер, получает его, сохраняет его в кеше и возвращает файл клиенту.Программирование сокетов Python для Webserver
Ниже приводится код:
from socket import *
import sys
if len(sys.argv) <= 1:
print 'Usage : "python ProxyServer.py server_ip"\n[server_ip : It is the IP Address Of Proxy Server'
sys.exit(2)
# Create a server socket, bind it to a port and start listening
tcpSerSock = socket(AF_INET, SOCK_STREAM)
tcpSerSock.bind((sys.argv[1], 8888))
tcpSerSock.listen(100)
while 1:
# Strat 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 wether the file exist 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 i in range(0, len(outputdata)):
tcpCliSock.send(outputdata[i])
print 'Read from cache'
# Error handling for file not found in cache
except IOError:
if fileExist == "false":
# Create a socket on the proxyserver
c = socket(AF_INET, SOCK_STREAM)
hostn = filename.replace("www.","",1)
print hostn
try:
# Connect to the socket to port 80
c.connect((hostn, 80))
# 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\n\n")
# Read the response into buffer
buff = 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 line in buff:
tmpFile.write(line);
tcpCliSock.send(line);
except:
print "Illegal request"
else:
# HTTP response message for file not found
tcpCliSock.send("HTTP/1.0 404 sendErrorErrorError\r\n")
tcpCliSock.send("Content-Type:text/html\r\n")
tcpCliSock.send("\r\n")
# Close the client and the server sockets
tcpCliSock.close()
tcpSerSock.close()
Но для каждого файла я прошу, я только получаю сообщение «недопустимый запрос» печать. Кажется, что проблема в том, что прокси-сервер фактически не может получить запрошенный файл клиентом. Может кто-то сказать мне, где я могу улучшить код. Это первый раз, когда я кодирую Python, поэтому, пожалуйста, укажите любые незначительные ошибки.
Я голосую, чтобы закрыть этот вопрос как не по теме, потому что он предназначен для отладки определенного фрагмента кода. Я считаю, что это будет по теме на http://codereview.stackexchange.com/ –
Согласно справочному центру обзора кода, этот сайт предназначен для «обратной связи по конкретному ** рабочему ** фрагменту кода» (акцент их) и явно не для «Устранение неполадок, отладка или понимание фрагментов кода». Я считаю, что StackOverflow - правильный сайт для этого (поскольку это кажется законной проблемой, а не «дайте мне код»), предполагая, что OP в основном заинтересована в разрешении этой ошибки «незаконного запроса». – Ixrec