python network_error ai_generated true

aiohttp.client_exceptions.TooManyRedirects: 重定向次数过多

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

ID: python/aiohttp-toomanyredirects

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

版本兼容性

版本状态引入弃用备注
3.9+ active — — —

根因分析

服务器返回重定向循环(或超过 max_redirects 跳数),通常由 HTTP 到 HTTPS 配置错误或 cookie/会话要求引起。

English

The server returns a redirect loop (or more than max_redirects hops), typically due to HTTP-to-HTTPS misconfiguration or cookie/session requirements.

generic

解决方案

  1. 88% 成功率
    async with session.get(url, allow_redirects=False) as r:
        print(r.status, r.headers.get('Location'))
    # follow manually and correct the base URL or auth cookie
  2. 75% 成功率
    session.cookie_jar.update_cookies({'session': token})
    async with session.get(url) as r:
        ...

无效尝试

常见但无效的做法:

  1. 80% 失败

    A true redirect loop never terminates; raising the limit only delays the failure and wastes requests.

  2. 65% 失败

    You then receive the 3xx response body instead of the target content, so the fetch still fails functionally.