python network_error ai_generated true

aiohttp.client_exceptions.ClientConnectorError: 无法连接到主机 api.example.com:443 ssl:default [连接调用失败]

aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host api.example.com:443 ssl:default [Connect call failed]

ID: python/aiohttp-cannot-connect-to-host

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

版本兼容性

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

根因分析

对目标主机的 DNS 解析或 TCP 连接失败,通常因网络限制、错误的主机/端口或防火墙阻止出站流量。

English

DNS resolution or TCP connection to the target host failed, often due to network restrictions, wrong host/port, or firewall blocking outbound traffic.

generic

解决方案

  1. 90% 成功率
    Configure a proxy if the environment requires one:
    
    async with aiohttp.ClientSession() as s:
        async with s.get(url, proxy='http://proxy.local:8080') as r:
            ...
  2. 85% 成功率
    Verify DNS and connectivity first:
    
    import socket
    socket.gethostbyname('api.example.com')
    # then retry with aiodns resolver if needed

无效尝试

常见但无效的做法:

  1. 80% 失败

    Connection is refused or unreachable; a longer timeout only delays the same error.

  2. 85% 失败

    The failure occurs before the TLS handshake; ssl=False does not help when TCP connect fails.