python network_error ai_generated true

aiohttp 客户端连接器错误:无法连接到主机 example.com:80 ssl:default [连接被拒绝]

aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host example.com:80 ssl:default [Connection refused]

ID: python/aiohttp-client-connector-connection-error

其他格式: JSON · Markdown 中文 · English
80%修复率
82%置信度
0证据数
2025-01-10首次发现

版本兼容性

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

根因分析

远程主机主动拒绝连接,可能是因为该端口上没有服务监听,或者防火墙阻止了连接。

English

The remote host actively refused the connection, either because no service is listening on that port or a firewall blocked it.

generic

解决方案

  1. 80% 成功率 Check if the service is running and the port is open
    import socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    result = sock.connect_ex(('example.com', 80))
    if result != 0:
        print('Port is closed')
  2. 75% 成功率 Use a fallback URL or service discovery
    try:
        async with session.get('http://example.com') as resp:
            pass
    except aiohttp.ClientConnectorError:
        async with session.get('http://backup.example.com') as resp:

无效尝试

常见但无效的做法:

  1. Retrying with the same parameters indefinitely 50% 失败

    If the service is down, retrying won't help and may waste resources.

  2. Changing the port to a common alternative without verification 60% 失败

    The service may not be running on the alternative port either.