我目前正在解析 URL 列表,如果其中一个抛出异常,我只想中断并继续处理下一个。
但是,我不断得到
OpenSSL.SSL.Error: [('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')]
解决此异常目前超出了我的专业知识,因此我希望能够暂时跳过它并稍后再处理。然而,我的问题是当我编写 try catch 来执行此操作时:
try:
r = requests_retry_session(session=s).get(test_link, verify=cafile, allow_redirects=True, timeout=4.0)
except OpenSSL.SSL.Error:
print('SSL error, moving to next domain')
break
我明白了:
NameError: name 'OpenSSL' is not defined
我知道解决此问题的最简单方法就是执行 verify=False
,但是,这非常不安全,我不想让自己面临可能的 MITM 攻击。
我尝试过的:
尝试follow the documentation of this previous related question :
pip install --upgrade certifi
最后是一个极其草率的 try catch block ,它本质上捕获了 NameError: name 'OpenSSL' is not Defined
并将其作为名称错误中断,这根本不是 pythonic。我知道我可以做一个非常笼统的包罗万象,但是,我不想跳过所有错误。
我正在运行 python 3.7.3 并请求 2.22.0
最佳答案
Attempting to follow the documentation of this previous related question
您应该按照这些说明并安装 pyopenssl
(pip install pyopenssl
) 并在以下位置导入其 OpenSSL
来解决此异常处理:模块顶部。
关于python - 识别并中断异常 : 'OpenSSL.SSL.Error' using python's requests module,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56836841/