python
network_error
ai_generated
partial
httpx.ConnectError: [Errno 111] 连接被拒绝
httpx.ConnectError: [Errno 111] Connection refused
ID: python/httpx-connect-error-all-connection-attempts-failed
80%修复率
83%置信度
0证据数
2024-09-02首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.8+ | active | — | — | — |
| 3.9+ | active | — | — | — |
| 3.10+ | active | — | — | — |
| 3.11+ | active | — | — | — |
| 3.12+ | active | — | — | — |
根因分析
目标主机/端口未接受 TCP 连接;服务器已关闭、端口错误或被防火墙阻止。
English
The target host/port is not accepting TCP connections; server is down, wrong port, or blocked by firewall.
解决方案
-
85% 成功率
import httpx, asyncio for delay in (1, 2, 4, 8): try: async with httpx.AsyncClient() as c: r = await c.get(url) break except httpx.ConnectError: await asyncio.sleep(delay) -
88% 成功率
transport = httpx.AsyncHTTPTransport(retries=3) async with httpx.AsyncClient(transport=transport) as c: r = await c.get(url)
无效尝试
常见但无效的做法:
-
90% 失败
Connection refused is immediate; timeouts are irrelevant.
-
85% 失败
The failure occurs before any HTTP exchange.