Question: Develop Web server and client programs in Python In this programming assignment, you will develop a pair of client and server programs using network socket
Develop Web server and client programs in Python
In this programming assignment, you will develop a pair of client and server programs using network socket programming for TCP connections in Python: how to create a socket, bind it to a specific address and port, connect to other application, and send and receive a HTTP packets. You will also learn some basics of HTTP header format. Use Python to develop your programs. Do not use any HTTP packages. See [KR] Section 2.7 for example TCP client and server codes.
Web Server and Client Programs
-
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 should send an HTTP 404 Not Found message back to the client.
In either case, the server should go back to the listening mode and repeat the accept and respond sequence. The server should terminate if an interrupt signal (^c) is received from the keyboard or the client sends Stop (without the quotes) as the file requested.
Below you will find the skeleton code for the Web server. Complete the skeleton code to get a rudimentary version of the server. 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.
However, the skeleton code does not check and catch most of the errors that may occur during execution. Revise the code to catch possible errors and handle them. Document your code thoroughly so that the logic is understandable by someone knowledgeable in networks but not an expert in Python or network application programming.
The format for running your Web server:
$ python3 server.py serverPort
-
A functional Web client is provided with some error checking but very little documentation. This Web client sends an HTTP GET request for a file to the server you developed in part 1, displays the response from the server and stops execution. You may use this client to test your Web server functionality. Modify the Web client so that Web server is throttled from sending the requested object at the maximum possible rate. For this purpose, use socket options to set receive buffer size to an appropriate value and modify the client application from reading the received data too quickly. Catch possible errors and document your code thoroughly.
The format for running your Web client: $ python3 client.py serverAddress serverPort fileName
Checking the Server Code
Put an HTML file (e.g., HelloWorld.html) in the same directory that the server is in. Run the server program that listens and accepts connection requests received at a port, say serverPort, specified through command line arguments. (The 16-bit port numbers are grouped into well- known ports, user-defined ports, and ephemeral ports. You need to use an appropriate for your server to use.)
You can check the working of your server program using a Web browser or wget command on Linux machines. For example, from another host or the same host, open a browser and provide the corresponding URL. For example:
http://10.100.240.204:serverPort/HelloWorld.html
HelloWorld.html is the name of the file you placed in the server directory. Note also the use of the port number after the colon. This is the port number at which the server application is listening and accepting clients connection requests. The browser should then display the contents of HelloWorld.html.
Then try to get a file that is not present at the server. You should get a 404 Not Found message.
Try stop the server by having the browser send a request to server for the URL: http://10.100.240.204:serverPort/stop Also, try to stop the server by sending a keyboard interrupt (^c) to the server process.
What to Hand in
-
Complete server code.
-
Complete client code.
-
Screenshots that demonstrate the working of your server program: client connection information, response to client, stopping the server, and handling any errors that occurred.
-
Screenshots of your client browser, verifying that you receive the contents of the HTML file from the server.
-
Screenshots that demonstrate the working of your client program: its request to the server and the response received, handling any errors that occurred, throttling of server from sending requested data quickly.
Skeleton Python Code for the Server.py
from socket import * #import socket module import sys # In order to terminate the program import errno import os
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 try:#Fill in end
message = #Fill in start #Fill in end filename = message.split()[1] f = open(filename[1:]) outputdata = #Fill in start #Fill in end #Send one HTTP header line into socket #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.send(" ".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()
sys.exit() #Terminate the program after sending the corresponding data
Example code for client.py (need to make a new one)
from socket import * # Import socket module import sys, os, errno
# Create a TCP server socket #(AF_INET is used for IPv4 protocols) #(SOCK_STREAM is used for TCP)
clientSocket = socket(AF_INET, SOCK_STREAM)
# Assign a port number serverPort = 6789
if len(sys.argv) < 4: print("Usage: python3 " + sys.argv[0] + " serverAddr serverPort filename") sys.exit(1) serverAddr = sys.argv[1] serverPort = int(sys.argv[2]) fileName = sys.argv[3]
# Connect to the server try: clientSocket.connect((serverAddr,serverPort)) except error as e: print("Connection to server failed. " + str(e)) sys.exit(1)
print('------The client is ready to send--------') print(str(clientSocket.getsockname()) + '-->' + str(clientSocket.getpeername()))
try: getRequest = "GET /" + fileName + " HTTP/1.1 Host: " + serverAddr + " " getRequest = getRequest + "Accept: text/html Connection: keep-alive " getRequest = getRequest + "User-agent: RoadRunner/1.0 " clientSocket.send(getRequest.encode()) # clientSocket.send(("GET /" + fileName + " HTTP/1.1 ").encode()) # clientSocket.send(("Host: " + serverAddr + " ").encode()) # clientSocket.send("Accept: text/html ".encode()) # clientSocket.send("Connection: keep-alive ".encode()) # clientSocket.send("User-agent: RoadRunner/1.0 ".encode()) except error as e: print("Error sending GET request: " + str(e)) clientSocket.close() sys.exit(1)
message = "" while True: try: newPart = clientSocket.recv(1024) message = message + newPart.decode() if not newPart: print (message, flush=True) break if message[len(message)-1] != " ": continue else: print(message, flush=True) message = "" except error as e: print('Error reading socket: ' + str(e)) sys.exit(1)
clientSocket.close() sys.exit(0)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
