Code Below you will find the skeleton code for the client Yo

Code Below you will find the skeleton code for the client. You are to complete the skeleton code. The places where you need to fill in code are marked with aFillin start and #Fill in end. Each place may require one or more lines of code.

Solution

from socket import *
import sys

#print len(sys.argv)
if len(sys.argv) <= 1:
    print \'Usage: \"python ProxyServer.py server_ip\"\ [server_ip : It is the IP Address of the Proxy Server\';
    sys.exit(2)

# Create a server socket, bind it to a port and start listening
tcpSerPort = 8888
tcpSerSock = socket(AF_INET, SOCK_STREAM)

# Fill in start

# Prepare a server socket
tcpSerSock.bind((\'\', tcpSerPort))
tcpSerSock.listen(5)

#Fill in end

while 1:
    # Start receiving data from the client
    print \'Ready to serve...\'
    tcpCliSock, addr = tcpSerSock.accept()
    print \'Received a connection from: \', addr
  
    message = tcpCliSock.recv(1024) #Fill in start # 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 (filename)
    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\ \ \")
        tcpCliSock.send(\"Content-Type:text/html\ \ \")
        #Fill in Start
      
        # Send the content of the requested file to the client
        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
            print \'Creating socket on 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))
                print \'Socket connected to port 80 of the host\'
                #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\")
                #fill in start
                for i in range(0, len(buff)):
                    tmpFile.write(buff[i])
                    tcpCliSock.send(buff[i])
                #fill in end
            except:
                print \'Illegal request\'

        else:
            # HTTP response message for file not found
            # Do stuff here
            #fill in start
            print \'File Not Found\'
            a = 2
            #fill in end
    # Close the socket and the server sockets
    tcpCliSock.close()

 Code Below you will find the skeleton code for the client. You are to complete the skeleton code. The places where you need to fill in code are marked with aFi
 Code Below you will find the skeleton code for the client. You are to complete the skeleton code. The places where you need to fill in code are marked with aFi

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site