I have the below code written in Python for a proxy server (which should be able to cache web pages. It is a very simple proxy server which only understands simple GET-requests, but is able to handle all kinds of objects–not just HTML pages, but also images), but when I try to run it it doesnt do anything. I have activated the proxy settings on windows. Here is the code below:
enter image description here
`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 OfProxy 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.
tcpSerSock.bind((sys.argv[1], 8888))
tcpSerSock.listen(5)
# Fill in end.
while 1:
# Strat receiving data from the client
print( 'Ready to serve...')
tcpCliSock, addr = tcpSerSock.accept()
print ('Received a connection from:', addr )
message = tcpCliSock.recv(1024).decode() # Fill in start.
print(message) # Fill in end.
# 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 wether 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 OKrn")
tcpCliSock.send("Content-Type:text/htmlrn")
# Fill in start.
for line in outputdata:
tcpCliSock.send(line.encode())
#f.close()
# 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
c = socket(AF_INET, SOCK_STREAM)# Fill in start. # 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.0nn")
# Read the response into buffer
# Fill in start.
buffer = fileobj.read()
# 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")
# Fill in start.
tmpFile.write(buffer)
tmpFile.close()
tcpCliSock.send(buffer.encode())
# Fill in end.
except:
print ("Illegal request")
else:
# HTTP response message for file not found
# Fill in start.
tcpCliSock.send("HTTP/1.0 404 Not Foundrn".encode())
tcpCliSock.send("Content-Type:text/htmlrn".encode())
tcpCliSock.send("rn".encode())
tcpCliSock.send("<html><body><h1>404 Not Found</h1></body></html>rn".encode())
# Fill in end.
# Close the client and the server sockets
tcpCliSock.close()
# Fill in start.
tcpSerSock.close()
# Fill in end’
Any help would be much appreciated
I tried the code on my computer, using cmd and i attached a
picture of the result. It doesnt seem to contiue
The Choumans is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2