python network_error ai_generated partial

httpx.ConnectError: [Errno 111] Connection refused

ID: python/httpx-connect-error-all-connection-attempts-failed

Also available as: JSON · Markdown · 中文
80%Fix Rate
83%Confidence
0Evidence
2024-09-02First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.8+ active
3.9+ active
3.10+ active
3.11+ active
3.12+ active

Root Cause

The target host/port is not accepting TCP connections; server is down, wrong port, or blocked by firewall.

generic

中文

目标主机/端口未接受 TCP 连接;服务器已关闭、端口错误或被防火墙阻止。

Workarounds

  1. 85% success
    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)
  2. 88% success
    transport = httpx.AsyncHTTPTransport(retries=3)
    async with httpx.AsyncClient(transport=transport) as c:
        r = await c.get(url)

Dead Ends

Common approaches that don't work:

  1. 90% fail

    Connection refused is immediate; timeouts are irrelevant.

  2. 85% fail

    The failure occurs before any HTTP exchange.