# httpx.ConnectTimeout: 连接到主机'api.example.com'超时（5000毫秒后），DNS解析失败

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

## 根因

DNS服务器无法解析主机名，可能是由于网络配置错误或DNS服务器不可用。

## 版本兼容性

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

## 解决方案

1. **Use an IP address directly instead of hostname** (70% 成功率)
   ```
   client = httpx.Client()
response = client.get('http://93.184.216.34')
   ```
2. **Configure a custom DNS resolver in the system or use a different DNS server** (90% 成功率)
   ```
   import socket
socket.setdefaulttimeout(10)
# Or change /etc/resolv.conf (Linux) or network settings (Windows)
   ```

## 无效尝试

- **Increasing timeout to a very high value** — Timeout increase does not fix DNS resolution; the request will still hang if DNS fails. (80% 失败率)
- **Restarting the application without checking DNS configuration** — The underlying DNS issue persists across restarts. (90% 失败率)
