#import socket module
from socket import *
import sys #In order to terminate the program

serverSocket = socket(AF_INET, SOCK_STREAM)
#Prepare a sever socket
serverSocket.bind(('', 6789)) #Binds local host to port 6789
serverSocket.listen(1) #Listens to a queued request

while True:
    #Establish the connection
    print('Ready to serve...')
    connectionSocket, addr = serverSocket.accept() #Accepts the connection
    try:
        message = connectionSocket.recv(1024).decode() #Receives buffer of size 1024
        filename = message.split()[1]
        f = open(filename[1:])
        outputdata = f.read() #Reads file f
        
        #Send one HTTP header line into socket
        connectionSocket.send('HTTP/1.1 200 OK\r\n\r\n')
        connectionSocket.send(outputdata)
        
        #Send the content of the requested file to the client
        for i in range(0, len(outputdata)):
            connectionSocket.send(outputdata[i].encode())
        connectionSocket.send("\r\n".encode())

        connectionSocket.close()
    except IOError:
        #Send response message for file not found
        connectionSocket.send('HTTP/1.1 404 Not Found\r\n')

        #Close client socket
        connectionSocket.close()
serverSocket.close()
sys.exit()#Terminate the program after sending the corresponding data

#import socket module
from socket import *
import sys #In order to terminate the program

serverSocket = socket(AF_INET, SOCK_STREAM)
#Prepare a sever socket
serverSocket.bind(('', 6789)) #Binds local host to port 6789
serverSocket.listen(1) #Listens to a queued request

while True:
    #Establish the connection
    print('Ready to serve...')
    connectionSocket, addr = serverSocket.accept() #Accepts the connection
    try:
        message = connectionSocket.recv(1024).decode() #Receives buffer of size 1024
        filename = message.split()[1]
        f = open(filename[1:])
        outputdata = f.read() #Reads file f
        
        #Send one HTTP header line into socket
        connectionSocket.send('HTTP/1.1 200 OK\r\n\r\n')
        connectionSocket.send(outputdata)
        
        #Send the content of the requested file to the client
        for i in range(0, len(outputdata)):
            connectionSocket.send(outputdata[i].encode())
        connectionSocket.send("\r\n".encode())

        connectionSocket.close()
    except IOError:
        #Send response message for file not found
        connectionSocket.send('HTTP/1.1 404 Not Found\r\n')

        #Close client socket
        connectionSocket.close()
serverSocket.close()
sys.exit()#Terminate the program after sending the corresponding data

#import socket module #In order to terminate the program #Prepare a sever socket #Binds local host to port 6789 #Listens to a queued request #Establish the connection #Accepts the connection #Receives buffer of size 1024 #Reads file f #Send one HTTP header line into socket #Send the content of the requested file to the client #Send response message for file not found #Close client socket #Terminate the program after sending the corresponding data