问题描述
我正在为一个任务制作一个 python 代理服务器。这是我使用 proxy.py 取得的进展。
from socket import *
import sys
import string
if len(sys.argv) <= 1:
print('\nUsage : "Python proxyServer.py server_ip"\nServer IP : Address of the Proxy Server')
sys.exit(2)
#create a server socket
SerSocket = socket(AF_INET,SOCK_STREAM)
print("\nSocket successfully Created")
port = 8888
host = ''
SerSocket.bind((host,port))
print("Socket is bound to port: %s" % port)
#listen for incoming connection
SerSocket.listen(5)
print("Socket is listening for incoming connections")
while True:
#receiving data from client
print("...Ready to Serve...")
#establish connection with client
CliSocket,Clientaddress = SerSocket.accept()
print('\nRecieve a connection from: ',Clientaddress)
#CliSocket.connect(Clientaddress)
message = CliSocket.recv(8)
#print(message.decode("utf-8"))
#extract filename from the given message
print(message.split()[1])
file1 = message.split()[1]
print(file1)
filename = message.decode("utf-8")
fileExist = "false"
#filename = "/" + filename
filetouse = "/" + filename.replace("/","")
print(filetouse)
try:
#check whether the file exist in cache
f = open(filetouse[1:],"r")
outputdata = f.readlines()
fileExist = "true"
#proxyServer finds a cache hit and generate response message
CliSocket.send(bytes("HTTP/1.0 200 OK\r\n","utf-8"))
#CliSocket.send("HTTP/1.0 200 OK\r\n")
CliSocket.send(bytes("content-Type: text/html\r\n"))
respond = ""
for s in outputdata:
respond += s
CliSocket.send(bytes(respond))
print('...read from cache')
#error handling for file not found in cache
except IOError:
if fileExist == "false":
#create a socket on the proxy server
c = socket(AF_INET,SOCK_STREAM)
hostn = filename.replace("www.","",1)
#hostn = filename.split('/'[0].replace("www.",1))
print(hostn)
try:
#connect to the socket to port 80
c.connect((hostn,port))
#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")
#show what request was made
print("GET "+"http://" + filename + " HTTP/1.0")
#read the response into buffer
respond = c.recv(4096)
response = ""
while respond:
response += respond
respond = c.recv(4096)
#create a new file in the cache for the requested file
#also send the response in the buffer to clientsocket & the corresponding file in the cache
if (filename[-1:] == '/'):
filename = filename[:-1]
tmp = open("./" + filename,"wb")
tmp.write(bytes(response))
tmp.close()
CliSocket.send(bytes(response))
except:
#print (str(e))
print("..Illegal Request")
else:
#HTTP response message for file not found
pass
#close the client and server socket
CliSocket.close()
我正在尝试运行 localhost:8888/www.google.com,但是无论何时我都会收到非法请求错误。一开始我以为这只是一个网站,但我尝试了一系列网站,所有网站都出现了同样的错误。
这是错误:
Receive a connection from: ('127.0.0.1',49623)
b'/www'
b'/www'
/GET www
GET /www
..Illegal Request
...Ready to Serve...
我该如何解决这个问题?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)