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

- **ID:** `python/aiohttp-client-connector-connection-error`
- **领域:** python
- **类别:** network_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.10 | active | — | — |
| 3.11 | active | — | — |

## 解决方案

1. **Check if the service is running and the port is open** (80% 成功率)
   ```
   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. **Use a fallback URL or service discovery** (75% 成功率)
   ```
   try:
    async with session.get('http://example.com') as resp:
        pass
except aiohttp.ClientConnectorError:
    async with session.get('http://backup.example.com') as resp:
   ```

## 无效尝试

- **Retrying with the same parameters indefinitely** — If the service is down, retrying won't help and may waste resources. (50% 失败率)
- **Changing the port to a common alternative without verification** — The service may not be running on the alternative port either. (60% 失败率)
