Question: Python help: I'm having trouble getting this HTTP web proxy server to work properly. I need this small web proxy server to be able to

Python help:

I'm having trouble getting this HTTP web proxy server to work properly. I need this small web proxy server to be able to cache web pages. This is a very simple proxy server which only understands simple GET-requests, but is able to handle all kinds of objects, such as images.

Generally, when the client makes a request, the request is sent to the web server. The web server then processes the request and sends back a response message to the requesting client. In order to improve the performance we create a proxy server between the client and the web server. Now, both the request message sent by the client and the response message delivered by the web server pass through the proxy server. In other words, the client requests the objects via the proxy server. The proxy server will forward the clients request to the web server. The web server will then generate a response message and deliver it to the proxy server, which in turn sends it to the client

NOTE: I have filled this code to the best of my ability but can not still get it to work. If I can get help on figuring out where I went wrong, that would be very helpful. I would also like to know how the correctly working code was run, like which Web browser was used and which python version was used.

Skeleton code was gathered from this site:

https://www.eecis.udel.edu/~cshen/450419/Socket4_ProxyServer.pdf

from socket import * import sys if len(sys.argv) <= 1: print 'Usage : "python ProxyServer.py server_ip" [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) # Fill in start tcpSerPort = 8888 tcpSerSock.bind(("", tcpSerPort)) tcpSerSock.listen(5) # Fill in end while 1: # Start receiving data from client print 'Ready to serve...' tcpCliSock, addr = tcpSerSpck.accept() print 'Received a connection from:', addr # Fill in start after the message = message = tcpCliSock.recv(1024) # Fill in end 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 whether 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 ") tcpCliSock.send("Content-Type:text/html ") # Fill in start for i in range (0, len(outputdata)): tcpCliSock.send(outputdata[i]) # Fill in end print 'Read from cache' # Error handling for file not found in cache except IOError: if fileExist == "false": # Create a socket on the proxyserver # Fill in start after c = c = socket(AF_INET, SOCK_STREAM) # Fill in end hostn = filename.replace("www.","",1) print hostn try: # Connect to the socket to port 80 # Fill in start c.connect(hostn, 80) # Fill in end # 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 ") # Read the response into buffer # Fill in start buff = fileobj.readlines() # Fill in end # 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 # File in start print '404 Error file not found' # Fill in end # Close the client and the sender sockets tcpCliSock.close() # Fill in start if _name_ == '_main_': main() # Fill in end

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!