# aiohttp.client_exceptions.ClientConnectorCertificateError: 无法连接到主机 api.example.com:443 ssl:True [SSLCertVerificationError: (1, '[SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败：无法获取本地颁发者证书 (_ssl.c:1006)')]

- **ID:** `python/aiohttp-cannot-connect-to-host-ssl`
- **领域:** python
- **类别:** network_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

客户端找不到用于验证服务器证书的 CA 证书包，常见于 macOS 官方 Python 构建或缺少 ca-certificates 的容器中。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   import ssl, certifi, aiohttp
ctx = ssl.create_default_context(cafile=certifi.where())
conn = aiohttp.TCPConnector(ssl=ctx)
async with aiohttp.ClientSession(connector=conn) as s:
    ...
   ```
2. **** (90% 成功率)
   ```
   # macOS
/Applications/Python\ 3.12/Install\ Certificates.command
   ```

## 无效尝试

- **** — Disables TLS verification entirely, exposing the app to MITM; often blocked by security policy. (40% 失败率)
- **** — The issue is the system trust store, not the aiohttp package. (90% 失败率)
