# httpx.ConnectError: [Errno 111] 连接被拒绝

- **ID:** `python/httpx-connect-error`
- **领域:** python
- **类别:** network_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

目标主机/端口未接受连接——服务已停止、绑定到其他接口或防火墙拦截。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   Verify the service is reachable:

curl -v http://host:port/
ss -tlnp | grep :port
   ```
2. **** (90% 成功率)
   ```
   In Docker Compose, use the service name (not localhost) as the host, and ensure depends_on with healthcheck.
   ```
3. **** (85% 成功率)
   ```
   Add a circuit breaker with tenacity and surface a clear error to callers.
   ```

## 无效尝试

- **** — If the service is down, retries fail identically. (85% 失败率)
- **** — TLS is not involved; the TCP connect fails before handshake. (90% 失败率)
- **** — Connection refused is immediate, not a timeout. (95% 失败率)
