简体   繁体   English

HTTP代理服务器python [客户端帮助]

[英]HTTP proxy server python [client side help]

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)? 目前,套接字编程非常新,但是您将如何制作一个与代理进行交互并通过代理将HTTP GET请求发送到多个Web服务器的客户端程序(例如:reddit.com,google.com,yahoo.com )? Also how would i run the proxy to test that it is working? 另外,我将如何运行代理以测试其是否正常工作?

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部分)

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()接受连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM