python network_error ai_generated true

aiohttp.client_exceptions.ServerTimeoutError: 连接主机 https://slow.example.com 超时

aiohttp.client_exceptions.ServerTimeoutError: Connection timeout to host https://slow.example.com

ID: python/aiohttp-connector-timeout

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

版本兼容性

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

根因分析

服务器未在配置的 aiohttp.ClientTimeout 内完成 TCP/TLS 握手或响应,通常是因为端点过载或超时设置过于激进。

English

The server did not complete the TCP/TLS handshake or respond within the configured aiohttp.ClientTimeout, often because of an overloaded endpoint or a too-aggressive default timeout.

generic

解决方案

  1. 90% 成功率
    timeout = aiohttp.ClientTimeout(total=30, connect=5, sock_read=10)
    async with aiohttp.ClientSession(timeout=timeout) as s:
        await s.get(url)
  2. 85% 成功率
    for i in range(3):
        try:
            async with session.get(url) as r:
                return await r.text()
        except aiohttp.ServerTimeoutError:
            await asyncio.sleep(2 ** i)

无效尝试

常见但无效的做法:

  1. 90% 失败

    Hangs forever on dead hosts and exhausts connection pool slots.

  2. 75% 失败

    Handshake still fails on the sock_connect phase before the total timer matters.