python network_error ai_generated true

aiohttp.client_exceptions.ClientConnectorCertificateError: 无法连接到主机 example.com:443 ssl:True [SSLCertVerificationError: 证书验证失败:无法获取本地颁发者证书]

aiohttp.client_exceptions.ClientConnectorCertificateError: Cannot connect to host example.com:443 ssl:True [SSLCertVerificationError: certificate verify failed: unable to get local issuer certificate]

ID: python/aiohttp-clientconnector-ssl-certificate-verify-failed

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

版本兼容性

版本状态引入弃用备注
3.8+ active — — —

根因分析

系统 CA 证书包缺失或过时,导致 aiohttp 默认的 SSL 上下文无法验证服务器证书链。

English

The system CA bundle is missing or outdated, so aiohttp's default SSL context cannot verify the server's certificate chain.

generic

解决方案

  1. 92% 成功率
    import ssl, certifi, aiohttp
    ctx = ssl.create_default_context(cafile=certifi.where())
    async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=ctx)) as s:
        await s.get('https://example.com')
  2. 85% 成功率
    sudo apt-get install --reinstall ca-certificates && sudo update-ca-certificates

无效尝试

常见但无效的做法:

  1. 60% 失败

    Disables all TLS verification, exposes to MITM and is rejected by many servers; masks the real CA problem.

  2. 55% 失败

    aiohttp uses its own default context, which on some platforms ignores certifi unless explicitly passed via ssl context.