python network_error ai_generated true

aiohttp.client_exceptions.TooManyRedirects: 0, message='重定向过多', url=URL('https://example.com/loop')

aiohttp.client_exceptions.TooManyRedirects: 0, message='Too many redirects', url=URL('https://example.com/loop')

ID: python/aiohttp-too-many-redirects

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

版本兼容性

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

根因分析

服务器返回了超过 max_redirects(默认 10)次重定向响应,通常由重定向循环或 cookie/认证配置错误引起。

English

The server returned more than max_redirects (default 10) redirect responses, often due to a redirect loop or cookie/auth misconfiguration.

generic

解决方案

  1. 90% 成功率
    Inspect the redirect chain with curl:
    
    curl -sIL https://example.com/loop | grep -i '^location'
  2. 85% 成功率
    Handle redirects manually to detect the loop:
    
    async with session.get(url, allow_redirects=False) as r:
        while r.status in (301, 302, 303, 307, 308):
            url = r.headers['Location']
            r = await session.get(url, allow_redirects=False)
  3. 82% 成功率
    Ensure cookies are sent (some redirect loops happen because auth cookie is missing):
    
    session.cookie_jar.update_cookies({'session': token})

无效尝试

常见但无效的做法:

  1. 70% 失败

    If it's a true loop, it will still hit 100 and fail.

  2. 50% 失败

    You now receive 3xx responses directly and must handle Location yourself.

  3. 90% 失败

    Unrelated to redirect count.