python network_error ai_generated partial

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

httpx.ConnectError: [Errno 104] Connection reset by peer while connecting to 'https://unstable.example.com'

ID: python/httpx-connecterror-connection-reset-by-peer

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

版本兼容性

版本状态引入弃用备注
3.x active

根因分析

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

English

The remote server abruptly closed the TCP connection, often due to server overload or network issues.

generic

解决方案

  1. 85% 成功率 Implement retry with exponential backoff and jitter
    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. 70% 成功率 Use a connection pool with keep-alive to reduce connection resets
    client = httpx.Client()
    response = client.get('https://unstable.example.com')

无效尝试

常见但无效的做法:

  1. Retrying immediately without any delay 80% 失败

    The server may still be overloaded, causing immediate reset again.

  2. Using a different HTTP version 90% 失败

    The issue is at the TCP level, not HTTP version.