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 in Python by anonymous
• 120 points
25 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 by Gitika
• 48,320 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,520 points
233 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,520 points
751 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 in Python by Justin
• 140 points

reshown Jun 18 by Sirajul 107 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
773 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,030 points
6,966 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
• 57,700 points
116 views
0 votes
1 answer

When I create and remove files rapidly on windows using python I get WindowsError (Error 5)

Here's the short answer: disable any antivirus or ...READ MORE

answered Aug 31, 2018 in Python by charlie_brown
• 7,780 points
699 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 in Python by Gitika
• 48,320 points
133 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 in Python by Gitika
• 48,320 points
362 views