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

- **ID:** `python/aiohttp-too-many-redirects`
- **领域:** python
- **类别:** network_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.8 | active | — | — |
| 3.9 | active | — | — |
| 3.10 | active | — | — |
| 3.11 | active | — | — |
| 3.12 | active | — | — |

## 解决方案

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})
   ```

## 无效尝试

- **** — If it's a true loop, it will still hit 100 and fail. (70% 失败率)
- **** — You now receive 3xx responses directly and must handle Location yourself. (50% 失败率)
- **** — Unrelated to redirect count. (90% 失败率)
