python network_error ai_generated true

aiohttp 客户端连接器 SSL 错误:证书验证失败

aiohttp.client_exceptions.ClientConnectorSSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed

ID: python/aiohttp-client-connector-ssl-error

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

版本兼容性

版本状态引入弃用备注
3.9 active
3.10 active

根因分析

远程服务器的 SSL 证书无效、已过期或不受系统 CA 包信任。

English

The SSL certificate of the remote server is invalid, expired, or not trusted by the system's CA bundle.

generic

解决方案

  1. 80% 成功率 Update system CA certificates
    pip install certifi
    # or update OS CA bundle
  2. 90% 成功率 Provide a custom SSL context with trusted certificates
    import ssl
    ssl_context = ssl.create_default_context(cafile='/path/to/cert.pem')
    async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=ssl_context)) as session:

无效尝试

常见但无效的做法:

  1. Disabling SSL verification entirely with ssl=False 30% 失败

    This bypasses security and may lead to man-in-the-middle attacks; also not recommended for production.

  2. Using a custom SSL context without proper certificates 60% 失败

    If the custom context is not configured correctly, the error persists.