python network_error ai_generated true

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

httpx.ConnectError: [Errno 111] Connection refused

ID: python/httpx-connect-error

其他格式: JSON · Markdown 中文 · English
80%修复率
88%置信度
0证据数
2024-05-30首次发现

版本兼容性

版本状态引入弃用备注
3.8 active — — —
3.9 active — — —
3.10 active — — —
3.11 active — — —
3.12 active — — —

根因分析

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

English

The target host/port is not accepting connections — the service is down, bound to a different interface, or a firewall is blocking.

generic

解决方案

  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.

无效尝试

常见但无效的做法:

  1. 85% 失败

    If the service is down, retries fail identically.

  2. 90% 失败

    TLS is not involved; the TCP connect fails before handshake.

  3. 95% 失败

    Connection refused is immediate, not a timeout.