我的代码中有以下函数,当我不在任何代理后面时,它工作得很好。事实上,甚至不提certifi default CA certificate
,如果我通过 verify=TRUE
就可以了,我猜,因为它以相同的方式工作。
def reverse_lookup(lat, long):
cafile=certifi.where()
params={'lat' : float(lat), 'lon' : float(long), 'format' : 'json',
'accept-language' : 'en', 'addressdetails' : 1}
response = requests.get("https://nominatim.openstreetmap.org/reverse", params=params, verify=cafile)
#response = requests.get("https://nominatim.openstreetmap.org/reverse", params=params, verify=True) <-- this works as well
result = json.loads(response.text)
return result['address']['country'], result['address']['state'], result['address']['city']
当我在我的企业基础架构中运行相同的代码时(我在代理后面),我在代码中做了一些小的改动,在 requests
中提到代理作为参数。方法:def reverse_lookup(lat, long):
cafile=certifi.where()
proxies = {"https" : "https://myproxy.com"}
params={'lat' : float(lat), 'lon' : float(long), 'format' : 'json',
'accept-language' : 'en', 'addressdetails' : 1}
response = requests.get("https://nominatim.openstreetmap.org/reverse", params=params, verify=cafile, proxies=proxies)
result = json.loads(response.text)
return result['address']['country'], result['address']['state'], result['address']['city']
但是,如果我设置 verify=True
,它会在不同时间给我这 3 个 SSL 错误中的一个。或 verify=certifi.where()
:CERTIFICATE_VERIFY_FAILED
UNKNOWN_PROTOCOL
WRONG_VERSION_NUMBER
只有当我使用
verify=False
完全绕过 SSL 验证时才会起作用。我的问题是:
https
通过代理请求,如果我绕过 SSL 验证可以吗? 任何帮助表示赞赏。在 Python 2.7.15 和 3.9 中测试的代码
最佳答案
Since I'm sending the https request via proxy, is it ok if I bypass SSL verification ?
您是否需要 HTTPS 提供的保护,即加密应用程序数据(如密码,以及完整的 URL)以防止中间的恶意人员嗅探或修改?如果您不需要保护,则可以绕过证书验证。
How to make the default context of SSL verification work in this case, when I'm behind proxy ?
代理正在执行 SSL 拦截,并在执行此操作时基于内部 CA 为该站点颁发新证书。如果这是预期的(即不是攻击),那么您需要从代理导入 CA 作为信任
verify='proxy-ca.pem'
.您的 IT 部门应该能够为您提供代理 CA。But it gives me one out of these 3 SSL errors at different times, if I set
verify=True
orverify=certifi.where()
:CERTIFICATE_VERIFY_FAILED UNKNOWN_PROTOCOL WRONG_VERSION_NUMBER
它应该只给你
CERTIFICATE_VERIFY_FAILED
.其他两个错误表示代理设置错误,通常设置为 https_proxy
至https://...
而不是 http://...
(也可以在您的代码中看到)。
关于ssl - Python的默认SSL证书上下文在代理后面时不能在请求方法中工作,否则工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67037192/