# httpx.HTTPStatusError: 对URL 'https://api.example.com/v1/data' 的客户端错误 '429 请求过多'

- **ID:** `python/httpx-httpstatuserror-429`
- **领域:** python
- **类别:** network_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

由于短时间内请求过多，服务器对客户端进行速率限制。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

1. **Implement exponential backoff and retry logic** (90% 成功率)
   ```
   import time
for attempt in range(5):
    try:
        response = httpx.get('https://api.example.com/v1/data')
        response.raise_for_status()
        break
    except httpx.HTTPStatusError as e:
        if e.response.status_code == 429:
            time.sleep(2 ** attempt)
        else:
            raise
   ```
2. **Add a custom User-Agent header to identify your application** (60% 成功率)
   ```
   headers = {'User-Agent': 'MyApp/1.0'}
httpx.get('https://api.example.com/v1/data', headers=headers)
   ```

## 无效尝试

- **Retrying immediately with the same request** — Retrying without delay will likely hit the same rate limit again. (95% 失败率)
- **Ignoring the error and continuing to send requests** — The server may temporarily block the client after repeated violations. (80% 失败率)
