Search code examples
pythondjangounicodepython-socketsdjango-runserver

UnicodeDecodeError with socket.getfqdn in Django runserver on Windows


I’m building a Django API. After transferring my project to a new server and attempting to run it with:

python manage.py runserver

I encountered the following exception:

File "", line 795, in getfqdn
    hostname, aliases, ipaddrs = gethostbyaddr(name)
                                 ^^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb3 in position 19: invalid start byte

I verified my hostname using the hostname command in the terminal. It returned: DESKTOP-KTU1TC2 (consists of only ASCII characters). I have also run this code, but everything works just fine.

>>>import socket
>>>socket.gethostbyname('127.0.0.1')
127.0.0.1
>>>socket.gethostbyname('DESKTOP-KTU1TC2')
(some ip address)

I have also tried to explicitly start the server with:

python manage.py runserver 127.0.0.1:8000

The error persists.

Edit: So I added a debug statement directly in the socket module:

    name = name.strip()
    if not name or name in ('0.0.0.0', '::'):
        name = gethostname()
    try:
        print(name, 'THATS THE NAME')
        hostname, aliases, ipaddrs = gethostbyaddr(name)
    except error:
        pass

When I ran the server with --verbose 3, I got this output:

System check identified no issues (0 silenced).
127.0.0.1 THATS THE NAME
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "P:\python312\Lib\threading.py", line 1073, in _bootstrap_inner
    self.run()
  File "P:\python312\Lib\threading.py", line 1010, in run
    self._target(*self._args, **self._kwargs)
  File "P:\python312\Lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "P:\python312\Lib\site-packages\django\core\management\commands\runserver.py", line 144, in inner_run
    run(
  File "P:\python312\Lib\site-packages\django\core\servers\basehttp.py", line 270, in run
    httpd = httpd_cls(server_address, WSGIRequestHandler, ipv6=ipv6)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "P:\python312\Lib\site-packages\django\core\servers\basehttp.py", line 77, in __init__
    super().__init__(*args, **kwargs)
  File "P:\python312\Lib\socketserver.py", line 457, in __init__
    self.server_bind()
  File "P:\python312\Lib\wsgiref\simple_server.py", line 50, in server_bind
    HTTPServer.server_bind(self)
  File "P:\python312\Lib\http\server.py", line 138, in server_bind
    self.server_name = socket.getfqdn(host)
                       ^^^^^^^^^^^^^^^^^^^^
  File "P:\python312\Lib\socket.py", line 796, in getfqdn
    hostname, aliases, ipaddrs = gethostbyaddr(name)
                                 ^^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb3 in position 19: invalid start byteas requested.

Seems like some part of 127.0.0.1 is being interpreted with invalid UTF-8 encoding. But the IP itself looks valid.

Environment Details: Python: 3.12 Django: 5.1.4

What should i try to resolve this issue? Thank you in advance


Solution

  • For anyone facing this issue, I resolved it by checking the hosts file on the server and discovered some non-ASCII characters. After removing these characters and flushing the IP cache, the issue was fixed, and the server started running without errors.