python network_error ai_generated true

aiohttp.client_exceptions.ClientResponseError: 429, message='请求过于频繁', url=URL('https://api.example.com/v1/items')

aiohttp.client_exceptions.ClientResponseError: 429, message='Too Many Requests', url=URL('https://api.example.com/v1/items')

ID: python/aiohttp-client-response-error

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

版本兼容性

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

根因分析

远程服务器返回了 4xx/5xx 状态码,且会话/请求配置了 raise_for_status=True,因此 aiohttp 抛出 ClientResponseError 而非返回响应。

English

The remote server returned a 4xx/5xx status and the session/request was configured with raise_for_status=True, so aiohttp raised ClientResponseError instead of returning the response.

generic

解决方案

  1. 88% 成功率
    import asyncio, random
    for attempt in range(5):
        async with session.get(url) as r:
            if r.status == 429:
                await asyncio.sleep((2 ** attempt) + random.random())
                continue
            r.raise_for_status()
            return await r.json()
  2. 90% 成功率
    sem = asyncio.Semaphore(5)
    async def fetch(u):
        async with sem:
            async with session.get(u) as r:
                r.raise_for_status()
                return await r.json()

无效尝试

常见但无效的做法:

  1. 75% 失败

    Hides 5xx and auth failures; downstream code receives HTML error pages parsed as JSON.

  2. 85% 失败

    Aggravates rate limiting and often escalates to a 403 block.