Cache Proxy Server in Python
<p>I have a homework assignment which involves implementing a proxy cache server in Python. The idea is to write the web pages I access to temporary files on my local machine and then access them as requests come in if they are stored. Right now the code looks like this:</p>

<pre><code>from socket import *
import sys

def main():
    #Create a server socket, bind it to a port and start listening
    tcpSerSock = socket(AF_INET, SOCK_STREAM) #Initializing socket
    tcpSerSock.bind(("", 8030)) #Binding socket to port
    tcpSerSock.listen(5) #Listening for page requests
    while True:
        #Start receiving data from the client
        print 'Ready to serve...'
        tcpCliSock, addr = tcpSerSock.accept()
        print 'Received a connection from:', addr
        message = tcpCliSock.recv(1024)
        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 exists 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\r\n")
            tcpCliSock.send("Content-Type:text/html\r\n")
            for data in outputdata:
                tcpCliSock.send(data)
            print 'Read from cache'
        except IOError: #Error handling for file not found in cache
            if fileExist == "false":

                c = socket(AF_INET, SOCK_STREAM) #Create a socket on the proxyserver
                hostn = filename.replace("www.","",1) 
                print hostn
                try:
                    c.connect((hostn, 80)) #https://docs.python.org/2/library/socket.html
                    # 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\r\n")
                    # Read the response into buffer
                    buffr = fileobj.readlines()
                    # 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 data in buffr:
                        tmpFile.write(data)
                        tcpCliSock.send(data)
                except:
                    print "Illegal request"
            else: #File not found
                print "404: File Not Found"
        tcpCliSock.close() #Close the client and the server sockets

main()
</code></pre>

<p>To test my code, I run the proxy cache on my localhost and set my browser proxy settings accordingly like so</p>

<p><a href="https://i.stack.imgur.com/6UTn3.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6UTn3.jpg" alt="enter image description here"></a></p>

<p>However, when I run this code and try to access google with Chrome, I'm greeting with an error page saying err_empty_response.</p>

<p>Stepping through the code with the debugger made me realizing it's failing on this line</p>

<pre><code>c.connect((hostn, 80))
</code></pre>

<p>and I have no idea why. Any help would be greatly appreciated.</p>

<p>P.S. I'm testing this with Google Chrome, Python 2.7, and Windows 10</p>