(IN PYTHON) Lab 1: Web Server Lab In this lab, you will learn the basics of socket programming for TCP connections in Py

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

(IN PYTHON) Lab 1: Web Server Lab In this lab, you will learn the basics of socket programming for TCP connections in Py

Post by answerhappygod »

(IN PYTHON)
Lab 1: Web Server Lab In this lab, you willlearn the basics of socket programming for TCP connections inPython: how to create a socket, bind it to a specific address andport, as well as send and receive a HTTP packet. You will alsolearn some basics of HTTP header format. You will develop a webserver that handles one HTTP request at a time. Your web servershould accept and parse the HTTP request, get the requested filefrom the server’s file system, create an HTTP response messageconsisting of the requested file preceded by header lines, and thensend the response directly to the client. If the requested file isnot present in the server, the server should send an HTTP “404 NotFound” message back to the client.
Code Below: you will find the skeleton code forthe Web server. You are to complete the skeleton code. The placeswhere you need to fill in code are marked with #Fill instart and #Fill in end. Each place mayrequire one or more lines of code.
-------------------skeleton code----------------------------
#import socket module
from socket import *
import sys # In order to terminate the program
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.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.encode())
connectionSocket.send("\r\n".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 correspondingdata
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply