1
Assigned: 1st October, 2022
Due: 10th October, 2022
University of Missouri – St. Louis
Department of Computer Science
(CMP SCI 4730 Computer Networks and Communications)
Lab Assignment 2B
Max Points: 40
1. Socket programming exercise [based on Assignment 1/Chapter 2 in the textbook]
This exercise involves development of a Web Server. This Web Server i) creates a
connection when contacted by a client (
owser), ii) receives HTTP requests from
this connection; iii) parses the request to find the file being requested iv) get the file
from the server file system and send the response over the TCP connection to the
owser. If the requested file is not available in your server, your server should
eturn, “404 file not found.”
The skeleton code for the server is given below. Complete the code and run the
server. Test it by sending requests from your
owser.
To run the server
Put a test HTML file (e.g., HelloWorld.html) in the same directory that the webserver
is in. Run the server program. Determine the IP address of the host that is running
the server (e.g., XXXXXXXXXXOn another host, open a
owser and provide the
co
esponding URL. For example: http:
XXXXXXXXXX:6789/HelloWorld.html
‘HelloWorld.html’ is the name of the file you placed in the server directory. Replace
port# 6789 with whatever port you have used in the server code (do not use
eserved port numbers). The
owser should then display the contents of
HelloWorld.html [take a screenshot].
Try to get a file that is not present on the server. You should get a “404 Not Found”
message [take a screenshot].
Submission
Submit both the screenshots and the completed code.
Skeleton code
from socket import *
import sys # to use system functions
# create a TCP server socket
serverSocket = socket(AF_INET, SOCK_STREAM)
#Fill in your code for binding and keeping your socket ready to listen
2
while True:
print(“The server is ready to receive”)
connectionSocket, addr = #Fill your code for accepting client connection
try:
#receive message request
message = #Fill your code here
#Extract the path of the requested object from the message
#The path is the second part of the HTTP header, identified by [1]
filename = message.split()[1]
#The extracted path of the HTTP requests include character ‘\’, so read
#the path from the second character
f = open(filename[1:])
#store the content of the requested file in a temporary buffer
outputdata = f.read()
#Send one HTTP header line into socket
connection Socket.send(“HTTP/ XXXXXXXXXXOK\r\n\r\n”.encode())
#Send the content of the requested file to the client (connection socket)
for i in range(0, len(outputdata)):
connectionSocket.send(outputdata[i].encode())
connectionSocket.send("\r\n".encode())
#close the client connection socket
connectionSocket.close()
except IOE
or:
#Send response message for file not found
#Fill your code here for the ‘404 Not Found’ message
#Close client socket
#Fill your code here
serverSocket.close()
sys.exit()
Note: You have a choice of writing your own HTTP client to test your server, instead
of using a
owser. Your client will connect to the server using a TCP connection,
send an HTTP request to the server, and display the server response as an output.
You can assume that the HTTP request sent is a GET method. The client should take
command line arguments specifying the server IP address or host name, the port at
which the server is listening, and the path at which the requested object is stored at
the server.
The following is an input command format to run the client.
client.py server_host server_port filename