获取 SSL:CERTIFICATE_VERIFY_FAILED 将代理与 python 请求一起使用时

Getting SSL: CERTIFICATE_VERIFY_FAILED when using proxy with python requests

我正在尝试使用请求库在 python 中实现代理,但我一遍又一遍地收到相同的错误。这是我的代码:

proxies = {
        'http': 'http://127.0.0.1:24000',
        'https': 'https://127.0.0.1:24000',
    }
    resp = requests.get('https://api.myip.com', proxies=proxies)
    print(resp.text)

我正在使用 Bright Data 的代理管理器,我怀疑我对代理的实施是错误的。我得到的错误是:

raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='api.myip.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1129)')))

我已经尝试过我在网上找到的解决方案,例如 verify=false,它适用于此 link 但不适用于我需要访问的其他解决方案,这就是为什么我正在寻找更安全的解决方案。

verify=False 是一种方法,但禁用这些警告的更好方法是使用此方法:

import urllib3

urllib3.disable_warnings()

如果您有自签名证书和密钥的副本,您可以按如下方式修改代码:

proxies = {
    'http': 'http://127.0.0.1:24000',
    'https': 'http://127.0.0.1:24000',
}

certificate_path = os.path.join(CACERT_PATH, 'cacert.pem')
key_path = os.path.join(CACERT_KEY, 'cacert.key')

resp = requests.get('https://api.myip.com',
                    proxies=proxies,
                    cert=(certificate_path, key_path))
print(resp.text)