python network_error ai_generated true

aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host api.internal:443 ssl:default [Name or service not known]

ID: python/aiohttp-cannot-connect-to-host-name-not-known

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2024-08-19First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.8 active — — —
3.9 active — — —
3.10 active — — —
3.11 active — — —
3.12 active — — —

Root Cause

DNS resolution failed for the hostname; inside containers this often means the resolver config (/etc/resolv.conf) or the hostname itself is wrong, or async DNS is blocked.

generic

中文

主机名的 DNS 解析失败;在容器中通常意味着解析配置(/etc/resolv.conf)或主机名本身有误,或者异步 DNS 被阻止。

Workarounds

  1. 90% success
    import socket
    socket.getaddrinfo('api.internal', 443)  # test from Python
    # then: cat /etc/resolv.conf and ensure the nameserver is reachable
  2. 82% success
    try:
        import aiodns
        conn = aiohttp.TCPConnector(resolver=aiohttp.AsyncResolver())
    except ImportError:
        conn = aiohttp.TCPConnector()  # default ThreadedResolver

Dead Ends

Common approaches that don't work:

  1. 90% fail

    The failure is DNS, not TLS; disabling SSL has no effect on name resolution.

  2. 85% fail

    Resolution fails immediately with NXDOMAIN; waiting longer does not help.