Question: You will develop a web server that handles one HTTP request at a time. Your web server should accept and parse the HTTP request, get
You will develop a web server that handles one HTTP request at a time. Your web server should accept and parse the HTTP request, get the requested file from the servers file system, create an HTTP response message consisting of the requested file preceded by header lines, and then send the response directly to the client. If the requested file is not present in the server, the server
1
should send an HTTP 404 Not Found message back to the client.
Code
Below you will find the skeleton code for the Web server. You are to complete the skeleton code. The places where you need to fill in code are marked with #Fill in start and #Fill in end. Each place may require one or more lines of code.
Skeleton Python Code for the Web Server
#import socket module
from socket import *
serverSocket = socket(AF_INET, SOCK_STREAM)
#Prepare a sever socket
#Fill in start
#Fill in end
while True:
#Establish the connection
print ('Ready to serve...')
connectionSocket, addr =
#Fill in start
#Fill in end
try:
message =
#Fill in start
#Fill in end
filename = message.decode().split()[1] f = open(filename[1:]) outputdata = #Fill in start
#Fill in end
#Send one HTTP header line into socket, make sure to use the b prefix to convert string to binary stream
# For example: b Some string
#Fill in start
#Fill in end
#Send the content of the requested file to the client
for i in range(0,len(outputdata)):
connectionSocket.send(outputdata[i].encode())
connectionSocket.close()
except IOError:
#Send response message for file not found
#Fill in start
#Fill in end
#Close client socket
#Fill in start
#Fill in end
serverSocket.close()
2
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
