You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Python下载完整的SSL证书链?

问题描述

我使用requests库调用API获取数据,代码如下:

def call_api():
   
    URL = f'......'
    API_KEY = "......"
    header = {
        "Authorization": API_KEY,
        "accept": "application/json",
        "cache-control": "no-cache",
    }

    s = requests.Session()
    s.headers = header
    s.verify = os.path.abspath("certificate.pem")
    s.mount(URL, MyAdapter())

    result = s.get(URL)
    result_json = result.json()

class MyAdapter(requests.adapters.HTTPAdapter):
    def init_poolmanager(self, connections, maxsize, block=False):
        self.poolmanager = urllib3.PoolManager(
            num_pools=connections,
            maxsize=maxsize,
            block=block,
            ssl_version=ssl.PROTOCOL_TLSv1_2,
        )

如果不指定证书,会出现以下SSL验证错误:

Max retries exceeded with url: ..... (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997)')))

我通过Google Chrome手动下载了证书链,在自己的电脑上可以正常使用,但换到其他设备就失效了。

我的核心问题是:如何使用Python下载完整的证书链?


已尝试方案的问题

我试过一种获取证书链的方法,但存在以下两个问题:

  • 该方法仅返回5个证书,而Chrome提取的证书链包含6个证书
  • 只有3个证书与Chrome下载的证书链内容匹配

内容的提问来源于stack exchange,提问作者Riolou

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 05:35:27