In: Computer Science
In this assignment, you will develop a simple Web server in Python that is capable of processing only one request. Specifically, your Web server will (i) create a connection socket when contacted by a client (browser); (ii) receive the HTTP request from this connection; (iii) parse the request to determine the specific file being requested; (iv) get the requested file from the server’s file system; (v) create an HTTP response message consisting of the requested file preceded by header lines; and (vi) send the response over the TCP connection to the requesting browser. If a browser requests a file that is not present in your server, your server should return a “404 Not Found” error message.
In the Companion Website, we provide the skeleton code for your server. Your job is to complete the code, run your server, and then test your server by sending requests from browsers running on different hosts. If you run your server on a host that already has a Web server running on it, then you should use a different port than port 80 for your Web server.
PLEASE ANSWER COMPLETELY FOR THUMBS UP. PLEASE DON'T LEAVE ANYTHING EMPTY.
from socket import *
serverPort=80
serverSocket = socket(AF_INET, SOCK_STREAM)
#server socket
serverSocket.bind(('',serverPort))
serverSocket.listen(1)
print 'the web server is up on port:',serverPort
#for connection
while True:
print 'Ready to serve...'
connectionSocket, addr = serverSocket.accept()
try:
message =
connectionSocket.recv(1024)
print message,'::',message.split()[0],':',message.split()[1]
filename = message.split()[1]
print filename,'||',filename[1:]
f = open(filename[1:])
outputdata = f.read()
print outputdata
#header line to socket
connectionSocket.send('\nHTTP/1.1 200
OK\n\n')
connectionSocket.send(outputdata)
#sending requested file
for i in range(0,
len(outputdata)):
connectionSocket.send(outputdata[i])
connectionSocket.close()
#message for file not found
except IOError:
connectionSocket.send('\nHTTP/1.1 404
Not Found\n\n')
connectionSocket.send('\nHTTP/1.1 404 Not Found\n\n')