# aiohttp 客户端连接器 DNS 错误：无法解析主机名 example.com

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

## 根因

给定主机名的 DNS 解析失败，可能是由于网络问题或无效域名。

## 版本兼容性

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

## 解决方案

1. **Check DNS configuration and use a custom resolver** (85% 成功率)
   ```
   from aiohttp.resolver import AsyncResolver
resolver = AsyncResolver(nameservers=['8.8.8.8', '8.8.4.4'])
connector = aiohttp.TCPConnector(resolver=resolver)
async with aiohttp.ClientSession(connector=connector) as session:
   ```
2. **Implement retry with DNS fallback** (80% 成功率)
   ```
   import socket
try:
    socket.gethostbyname('example.com')
except socket.gaierror:
    # fallback to IP or alternative hostname
   ```

## 无效尝试

- **Using IP address directly instead of hostname** — IP addresses may change, and SSL certificates may not match, causing other errors. (50% 失败率)
- **Adding a custom hosts file entry without proper validation** — The entry may be incorrect or overridden by system settings. (40% 失败率)
