0

I got a simple example server file from my lecturer. It is working fine for the other students, but when I am trying to run it I get this error:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf8 in position 2: invalid start byte

The error points to this line in the code, where server address is ('127.0.0.1', 8080) and the other variable is a class with "do_GET" and "do_POST" methods:

 httpd = HTTPServer(server_address, myHTTPServer_RequestHandler)

And ultimately points to this:

File "C:\Users\hetle\Anaconda3\envs\untitled\lib\socket.py", line 673, in getfqdn
hostname, aliases, ipaddrs = gethostbyaddr(name)

I am using Anaconda Version 5.1 with Python Version 3.6. I have also tried the standard Python interpreter, but with the same error. And I have made sure no other server is running at the same time.

I have tried shutting down all programs I know are running in the background, restarting the computer, looked in task manager(and tried shutting down some tasks), tried a different Directory(Documents). I have even tried a doing a "fresh" install of Windows 10 (but kept my files).

The strangest part is that if I change the IP address for the server to 127.0.0.2, it works just fine. And yes, I have asked a student Assistant via email (which led to nothing), and asked lecturer in person, and he had never seen such an error.

I have found out that I get a reply when pinging 127.0.0.1, and also from 127.0.0.2.

The reason I cant just use 127.0.0.2 is that I have an assignment that requires the use of a server (for testing purposes), which is using flask, and I cant (as far as I know) change the IP of that server.

Im completely sure the problem is not in the code (as it works for other students), and considering I have reinstalled Windows 10 which removed all apps and programs, in addition to all Windows settings turned back to default, I have no idea what the problem might be.

Should 127.0.0.1 reply to a ping, without me doing anything after a "fresh" install of Windows? If not, how can I find out what is replying? If yes, what can be the problem? Could there be something wrong with my hardware, low Level Windows files or something else?

def main():
    server_address = ('127.0.0.1', 8080)
    httpd = HTTPServer(server_address, myHTTPServer_RequestHandler)
    print("running server...")
    httpd.serve_forever()
Traceback (most recent call last):  
  File "C:/Users/hetle/Onedrive-UIS/OneDrive – Universitetet i Stavanger/DAT310 WebP/PyCharm-Projects/Exercises/Web-protocols/server.py", line 48, in <module>
    main()  
  File "C:/Users/hetle/Onedrive-UIS/OneDrive – Universitetet i Stavanger/DAT310 WebP/PyCharm-Projects/Exercises/Web-protocols/server.py", line 42, in main
    httpd = HTTPServer(server_address, myHTTPServer_RequestHandler)  
  File "C:\Users\hetle\Anaconda3\envs\untitled\lib\socketserver.py", line 453, in __init__
    self.server_bind()  
  File "C:\Users\hetle\Anaconda3\envs\untitled\lib\http\server.py", line 138, in server_bind
    self.server_name = socket.getfqdn(host)  
  File "C:\Users\hetle\Anaconda3\envs\untitled\lib\socket.py", line 673, in getfqdn
    hostname, aliases, ipaddrs = gethostbyaddr(name)   
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf8 in position 2: invalid start byte
davidism
  • 121,510
  • 29
  • 395
  • 339
  • 1
    If you go to http://127.0.0.1 in your browser, do you see another application running there such as Windows IIS? – JHS Mar 18 '18 at 22:30

2 Answers2

0

I don't have a windows machine available so I can't do some checks, but it sounds like your computer's name might have some "special" characters and Python is not liking it.

Edit:
There seems to be indeed an existing bug that is all about your issue: https://bugs.python.org/issue9377

My suggestion is:

  • Set a name for 127.0.0.1 in your hosts file (C:\Windows\System32\Drivers\etc\hosts).
    Make sure that the name only contains ASCII (non-extended) characters.
  • If that doesn't get around the issue, try modifying your hostname also to something that uses ASCII characters only.
lubumbax
  • 255
  • 2
  • 9
0

You are using the Unicode of python

File "C:\Users\hetle\Anaconda3\envs\untitled\lib\socket.py"

Here \U is a kind of Unicode, you have used in C:\Users. So python treats is as Unicode, so just Transpose it as / you will get rid of this problem.

File "C:/Users/hetle/Anaconda3/envs/untitled/lib/socket.py"

Example : >>

>>> s = u'La Pe\xf1a'         1
>>> print s                   2
Traceback (innermost last):
  File "<interactive input>", line 1, in ?
UnicodeError: ASCII encoding error: ordinal not in range(128)
>>> print s.encode('latin-1') 3
La Peña

Example 2:>>

>>> title                                       2
u'\u041f\u0440\u0435\u0434\u0438\u0441\u043b\u043e\u0432\u0438\u0435'
>>> print title                                 3
Traceback (innermost last):
  File "<interactive input>", line 1, in ?
UnicodeError: ASCII encoding error: ordinal not in range(128)
>>> convertedtitle = title.encode('koi8-r')     4
>>> convertedtitle
'\xf0\xd2\xc5\xc4\xc9\xd3\xcc\xcf\xd7\xc9\xc5'
>>> print convertedtitle                        5
Предисловие
MrBhuyan
  • 151
  • 2
  • 17