python network_error ai_generated true

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

aiohttp.client_exceptions.ClientConnectorCertificateError: Cannot connect to host api.example.com:443 ssl:True [SSLCertVerificationError: (1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1006)')]

ID: python/aiohttp-cannot-connect-to-host-ssl

其他格式: JSON · Markdown 中文 · English
80%修复率
87%置信度
0证据数
2024-02-14首次发现

版本兼容性

版本状态引入弃用备注
3.8+ active
3.9+ active
3.10+ active
3.11+ active
3.12+ active

根因分析

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

English

The client cannot find the CA bundle to verify the server certificate, often on macOS Python.org builds or in containers missing ca-certificates.

generic

解决方案

  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

无效尝试

常见但无效的做法:

  1. 40% 失败

    Disables TLS verification entirely, exposing the app to MITM; often blocked by security policy.

  2. 90% 失败

    The issue is the system trust store, not the aiohttp package.