Below you will find the skeleton code for the Web server. You are to complete th
ID: 3681807 • Letter: B
Question
Below you will find the skeleton code for the Web server. You are to complete the skeleton code or build your own code. In the skeleton code, the places where you need to fill in code are marked with #Fill. Each place may require one or more lines of code. (You may need to add additional lines other than the places marked with #Fill. The following just provides an example, you may not follow the example)
Skeleton Python Code for the Web Server
#import socket module
from socket import *
serverSocket = socket(AF_INET, SOCK_STREAM)
#Prepare a sever socket
#Fill
while True:
#Establish the connection
print 'Ready to serve...'
connectionSocket, addr = #Fill
try:
message = #Fill nd
filename = message.split()[1]
f = open(filename[1:])
outputdata = #Fill
#Send one HTTP header line into socket
#Fill
#Send the content of the requested file to the client
for i in range(0, len(outputdata)):
connectionSocket.send(outputdata[i])
connectionSocket.close()
except IOError:
#Send response message for file not found
#Fill
#Close client socket
#Fill
serverSocket.close()
#import socket module
from socket import *
serverSocket = socket(AF_INET, SOCK_STREAM)
#Prepare a sever socket
#Fill
while True:
#Establish the connection
print 'Ready to serve...'
connectionSocket, addr = #Fill
try:
message = #Fill nd
filename = message.split()[1]
f = open(filename[1:])
outputdata = #Fill
#Send one HTTP header line into socket
#Fill
#Send the content of the requested file to the client
for i in range(0, len(outputdata)):
connectionSocket.send(outputdata[i])
connectionSocket.close()
except IOError:
#Send response message for file not found
#Fill
#Close client socket
#Fill
serverSocket.close()
Explanation / Answer
#import socket module
from socket import *
serverSocket= socket(AF_INET, SOCK_STREAM)#creates socket
#Prepare a sever socket
#Fill
serverPort = 7000 #sets port
serverSocket.bind((”, serverPort)) #associates socket with this port
serverSocket.listen(1) #tells socket to listen for requests
#Fill
while True:
#Establish the connection
print ‘Ready to serve…’
connectionSocket, addr = serverSocket.accept() #creates a socket specifically for this client
try:
message =connectionSocket.recv(1024) #receives message from client
print message
filename = message.split()[1]
f = open(filename[1:]) #opens file and reads the contents
outputdata =f.read()
#Send one HTTP header line into socket
#Fill
connectionSocket.send(‘ HTTP/1.x 200 OK ’) #sends a 200 OK header line
#Fill
#Send the content of the requested file to the client
for i in range(0, len(outputdata)):
connectionSocket.send(outputdata[i]) #outputs all of the data in the file
connectionSocket.close() #closes the socket for this client
print ‘File Recieved’
except IOError:
#Sendresponse message for file not found
#Fill
connectionSocket.send(‘ 404 File Not Found ’) #sends an error message to be printed on the page
#Fill
#Close client socket
#Fill
connectionSocket.close() #closes the socket for the client
#Fill
serverSocket.close() #closes the server socket
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.