2015-05-05 61 views
1

我认为这将是很酷的实施这个代理服务器。目前对于套接字编程来说是非常新颖的东西,但是如何开发一个客户端程序来与代理交互,并且会通过代理向多个Web服务器发送HTTP GET请求(例如:reddit.com,google.com,yahoo.com )?另外我如何运行代理来测试它正在工作?HTTP代理服务器python [客户端帮助]

代码为代理:

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() 

使用OS X 10.10.3和Python 3.4

链接到代码的屏幕截图,以及: https://smartedblog.files.wordpress.com/2013/05/pr4-1.png(部分1) https://smartedblog.files.wordpress.com/2013/05/pr4-2.png(第2部分)

+0

这应该是一个HTTP代理?如果这样的协议是记录,为什么你需要一个特殊的客户端。尝试'curl'用'--proxy'选项来测试。 – lmz

+0

所以我应该打开2个终端之一与运行代理(文件名Proxyserver.py):python Proxyserver.py,我应该如何格式化第二个命令来调用和测试代理? –

+0

'curl --proxy“http://127.0.0.1:8888”“http://your.url.here/”' – lmz

回答

0

你有一个永不休息的while循环。

while 1: 
    print 'Ready to serve...' 

什么是您的代理输出?它看起来应该只是打印该行ad-infinitum。你的代理永远不会去这条线tcpCliSock, addr = tcpSerSock.accept()接受连接。

+0

客户端发送HTTP GET请求后,代理应该输出一个网页。由于无法连接某些客户端程序,因此我无法连接到某个客户端程序 –

+0

@MattMontalbano我明白了,但是您写它的方式,代理不会接受连接,因为它从来没有打过'tcpCliSock'行, addr = tcpSerSock.accept()'。你可以在启动时验证代理输出的内容吗? – chemdt