Help please on basic python web server

0 votes
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
Oct 25, 2020 in Python by anonymous
• 120 points
1,882 views

1 answer to this question.

0 votes

Hi, @There,

The error is because there is some non-Ascii character in the dictionary and it can't be encoded/decoded. One simple way to avoid this error is to encode such strings with encode() function as follows (if a is the string with non-Ascii character):

a.encode('utf-8').strip()
answered Oct 27, 2020 by Gitika
• 65,770 points

Related Questions In Python

0 votes
1 answer

Need help installing easy_install in Python 2.7.1 on Windows 7

That tool is part of the setuptools ...READ MORE

answered Dec 26, 2018 in Python by Nymeria
• 3,560 points
1,199 views
0 votes
1 answer

Need help bootstrapping Python module installation on Amazon EMR

Hello! The easiest way to definitely do this ...READ MORE

answered Feb 11, 2019 in Python by Nymeria
• 3,560 points
2,545 views
0 votes
0 answers

For some reason i cannot click on this element/button and have been trying for hours PLEASE HELP!!

from selenium import webdriver from selenium.webdriver.chrome.options import Options from ...READ MORE

Jun 18, 2020 in Python by Justin
• 140 points

reshown Jun 18, 2020 by Sirajul 766 views
0 votes
1 answer

can someone please help me with python?

Hello @ Anee, Code: num1 = int(input("Enter First Number: ")) num2 ...READ MORE

answered Nov 30, 2020 in Python by Niroj
• 82,840 points
626 views
0 votes
1 answer

How to perform web scraping with python?

Hey, there are various libraries used in ...READ MORE

answered Apr 20, 2018 in Python by aayushi
• 750 points
1,795 views
+1 vote
2 answers

Measuring the distance between pixels on OpenCv with Python

You can try this: Mat pts1(nPts, 1, CV_8UC2), ...READ MORE

answered Aug 24, 2018 in Python by Omkar
• 69,220 points
10,554 views
0 votes
1 answer

Best way to create a simple python web service

web.py is probably the simplest web framework ...READ MORE

answered Jul 20, 2018 in Python by Priyaj
• 58,020 points
909 views
0 votes
1 answer

what is vm options ? I installed Pycharm on my pc but it shows no vm options file found . What should i do please help me to fix it

Hi, @Mushfiqkhantrial, It is important to run pycharm.sh from the bin folder. The VM ...READ MORE

answered Oct 4, 2020 in Python by Gitika
• 65,770 points
1,823 views
0 votes
1 answer

Please Help me About this Pygame Error installing on pip

Hi, @Micheal, Download pygame‑1.9.3‑cp37‑cp37m‑win_amd64. While if you have a 64-bit ...READ MORE

answered Oct 12, 2020 in Python by Gitika
• 65,770 points
3,359 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP