# httpx.ConnectError: [Errno 104] 连接被对端重置，连接到'https://unstable.example.com'时

- **ID:** `python/httpx-connecterror-connection-reset-by-peer`
- **领域:** python
- **类别:** network_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

远程服务器突然关闭TCP连接，通常是由于服务器过载或网络问题。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

1. **Implement retry with exponential backoff and jitter** (85% 成功率)
   ```
   import time, random
for i in range(3):
    try:
        response = httpx.get('https://unstable.example.com')
        break
    except httpx.ConnectError:
        time.sleep(2 ** i + random.uniform(0, 1))
   ```
2. **Use a connection pool with keep-alive to reduce connection resets** (70% 成功率)
   ```
   client = httpx.Client()
response = client.get('https://unstable.example.com')
   ```

## 无效尝试

- **Retrying immediately without any delay** — The server may still be overloaded, causing immediate reset again. (80% 失败率)
- **Using a different HTTP version** — The issue is at the TCP level, not HTTP version. (90% 失败率)
