python network_error ai_generated true

httpx.ConnectError: [Errno -2] Name or service not known while connecting to 'nonexistent.example.com'

ID: python/httpx-connecterror-dns-resolution-failure

Also available as: JSON · Markdown · 中文
80%Fix Rate
84%Confidence
0Evidence
2025-02-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

The hostname cannot be resolved by the system's DNS resolver.

generic

中文

系统DNS解析器无法解析主机名。

Workarounds

  1. 85% success Use a working DNS server like Google's 8.8.8.8
    # On Linux: echo 'nameserver 8.8.8.8' > /etc/resolv.conf
    # Or use socket.create_connection with IP address
  2. 70% success Replace hostname with IP address in the request
    httpx.get('http://93.184.216.34')  # example.com IP

Dead Ends

Common approaches that don't work:

  1. Using a different DNS resolver in code without system changes 80% fail

    httpx uses the system resolver by default; custom resolver requires extra setup.

  2. Ignoring the error and retrying 90% fail

    DNS failure persists across retries.