I thought it would be cool to implement this proxy server. Currently very new to socket programming but how would you go about making a client side program which will interact with the proxy and will send HTTP GET requests to multiple web servers through the proxy (ex: reddit.com, google.com, yahoo.com)? Also how would i run the proxy to test that it is working?
我认为实现这个代理服务器会很酷。目前对套接字编程很新,但是你将如何制作一个与代理交互的客户端程序,并通过代理向多个Web服务器发送HTTP GET请求(例如:reddit.com,google.com,yahoo.com )?另外我如何运行代理来测试它是否正常工作?
Code for the proxy:
代理代码:
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)
tcpSerPort = 8888
tcpSerSock.bind(("", tcpSerPort))
tcpSerSock.listen(5)
while 1:
#start recieving data from the client
print 'Ready to serve...'
tcpCliSock, addr = tcpSerSock.accept()
print 'Recieved a connection from:', addr
message = tcpCliSock.recv(1024)
print message
#extract the filename from a 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 exists in the cache
f = open(filetouse[1:], "r")
outputdata = f.readlines()
fileExist = "true"
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 handeling for file not in cache
except IOError:
if fileExist == "false":
#creates 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)
#creates 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
#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 i in range(0, len(buff)):
tmpFile.write(buff[i])
tcpCliSock.send(buff[i])
except:
print "Illegal Request"
else:
#HTTP response message for file not found
print "404 Error file not found"
#close the client and the server sockets
tcpCliSock.close()
if __name__ == '__main__':
main()
Using OS X 10.10.3 and Python 3.4
使用OS X 10.10.3和Python 3.4
Links to screenshots of the code as well: https://smartedblog.files.wordpress.com/2013/05/pr4-1.png (part 1) https://smartedblog.files.wordpress.com/2013/05/pr4-2.png (part 2)
链接到代码的屏幕截图:https://smartedblog.files.wordpress.com/2013/05/pr4-1.png(第1部分)https://smartedblog.files.wordpress.com/2013/05/ pr4-2.png(第2部分)
1 个解决方案
#1
You have a while loop that never breaks.
你有一个永不休息的while循环。
while 1:
print 'Ready to serve...'
What is your proxy output? It looks like it should just print that line ad-infinitum. Your proxy never gets to this line tcpCliSock, addr = tcpSerSock.accept()
to accept a connection.
你的代理输出是什么?它看起来应该只打印无限行。您的代理永远不会到达此行tcpCliSock,addr = tcpSerSock.accept()来接受连接。
#1
You have a while loop that never breaks.
你有一个永不休息的while循环。
while 1:
print 'Ready to serve...'
What is your proxy output? It looks like it should just print that line ad-infinitum. Your proxy never gets to this line tcpCliSock, addr = tcpSerSock.accept()
to accept a connection.
你的代理输出是什么?它看起来应该只打印无限行。您的代理永远不会到达此行tcpCliSock,addr = tcpSerSock.accept()来接受连接。