在python请求中使用代理时获取SSL:CERTIFICATE\u VERIFY\u失败

2024-05-20 22:17:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用请求库在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,它适用于此链接,但不适用于我需要访问的其他链接,这就是为什么我在寻找更安全的解决方案


Tags: httpscomapihttp代理request错误certificate
2条回答

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

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)

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

import urllib3

urllib3.disable_warnings()

相关问题 更多 >