Search code examples
pythonlocalhostwindows-10anacondagethostbyaddr

Why can I run a python server on 127.0.0.2 but not on 127.0.0.1? UnicodeDecodeError


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

Solution

  • 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.