I been stuck on this problem for hours and I simply cannot get it to work. I am trying to run this python code to display the contents of HelloWorld.html on my localhost browser.
The line
f = open(filename[1:])
is getting me the error:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf7 in position 1: invalid start byte
#import socket module
from socket import *
import sys # In order to terminate the program
serverSocket = socket(AF_INET, SOCK_STREAM)
#Prepare a sever socket
# Binding our socket to the port
serverSocket.bind(('', 80))
serverSocket.listen(1)
while True:
# establish the connection
print('Ready to serve...')
connectionSocket,addr = serverSocket.accept()
try:
message = connectionSocket.recv(1024)
filename = message.split()[1]
f = open(filename[1:])
outputdata =f.read()
#Send one HTTP header line into socket
connectionSocket.send(b'HTTP/1.0 200 OK\r\n\r\n')
#Send the content of the request file to the client
for i in range(0, len(outputdata)):
connectionSocket.send(outputdata[i].encode())
connectionSocket.send(b"\r\n".encode)
connectionSocket.close()
except IOError:
#Send response message for file not found
connectionSocket.send(b'404 file Not Found')
#Close client socket
connectionSocket.close()
serverSocket.close()
sys.exit() #Terminating program after sending corresponding data