# aiohttp 客户端异常：超过 30 次重定向

- **ID:** `python/aiohttp-client-connector-http-redirect-error`
- **领域:** python
- **类别:** network_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

服务器在循环中重定向客户端或重定向次数过多，超过了默认重定向限制。

## 版本兼容性

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

## 解决方案

1. **Set max_redirects to a reasonable limit and handle redirect loops** (85% 成功率)
   ```
   async with session.get(url, max_redirects=5) as resp:
    # process response
   ```
2. **Disable automatic redirects and handle manually with loop detection** (80% 成功率)
   ```
   async with session.get(url, allow_redirects=False) as resp:
    if resp.status in (301, 302):
        new_url = resp.headers['Location']
        # check for loop before following
   ```

## 无效尝试

- **Increasing the max redirects to a very high number** — This may cause infinite loops or resource exhaustion. (50% 失败率)
- **Following redirects manually without checking for loops** — Manual following may still get stuck in a redirect loop. (60% 失败率)
