Question: This is my code: #import socket module from socket import * import sys # In order to terminate the program serverSocket = socket(AF_INET, SOCK_STREAM) #Prepare
This is my 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 serverPort = 6788 serverSocket.bind(('',serverPort)) serverSocket.listen(5) #Fill in end while True: #Establish the connection print('Ready to serve...') connectionSocket, addr = serverSocket.accept() #Fill in start #Fill in end try: message = connectionSocket.recv(1024) #Fill in start #Fill in end filename = message.split()[1] f = open(filename[1:]) outputdata = f.read() #Fill in start #Fill in end #Send one HTTP header line into socket #Fill in start connectionSocket.send(' HTTP/1.1 200 OK ') #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 connectionSocket.send('HTTP/1.1 404 Not Found ') #Fill in end #Close client socket #Fill in start connectionSocket.close() #Fill in end serverSocket.close() sys.exit()#Terminate the program after sending the corresponding data
This is my HTML file:
Hello World
I am getting this error:
Traceback (most recent call last): File "web_server.py", line 23, in
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
