#!python#import socket modulefrom socket import*import sys # In order to terminate the program
serverSocket = socket(AF_INET, SOCK_STREAM)#Prepare a sever socket
serverPort=8080
serverSocket.bind(('',serverPort))
serverSocket.listen(10)whileTrue:#Establish the connectionprint('Ready to serve...')
connectionSocket, addr = serverSocket.accept()try:
message = connectionSocket.recv(204800000).decode()
filename = message.split()[1]
f =open(filename[1:])
outputdata = f.read()#Send one HTTP header line into socket
header =' HTTP/1.1 200 OK\nConnection: close\nContent-Type: text/html\nContent-Length: %d\n\n'%(len(outputdata))
connectionSocket.send(header.encode())#Send the content of the requested file to the clientfor i inrange(0,len(outputdata)):
connectionSocket.send(outputdata[i].encode())
connectionSocket.send("\r\n".encode())
connectionSocket.close()except IOError:#Send response message for file not found
header =' HTTP/1.1 404 Not Found'
connectionSocket.send(header.encode())#Close client socket
connectionSocket.close()
serverSocket.close()
sys.exit()#Terminate the program after sending the corresponding data