python network_error ai_generated true

aiohttp.client_exceptions.ClientConnectorError: 无法连接到主机 api.internal:443 ssl:default [名称或服务未知]

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

其他格式: JSON · Markdown 中文 · English
80%修复率
85%置信度
0证据数
2024-08-19首次发现

版本兼容性

版本状态引入弃用备注
3.8 active — — —
3.9 active — — —
3.10 active — — —
3.11 active — — —
3.12 active — — —

根因分析

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

English

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

解决方案

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

无效尝试

常见但无效的做法:

  1. 90% 失败

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

  2. 85% 失败

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