我正在尝试使用 requests.py 库来调用休息网络服务。我为我在 windows 下的使用编写了一个快速原型,一切正常,但是当我尝试在 linux 下运行相同的原型时,我收到“requests.exceptions.Timeout: Request timed out”错误。有谁知道为什么会发生这种情况?如果我尝试使用该库访问非 https url,它在 windows 和 linux 下都可以正常工作。
import requests
url = 'https://path.to.rest/svc/?options'
r = requests.get(url, auth=('uid','passwd'), verify=False)
print(r.content)
我确实注意到,如果我从 get 调用中删除 verify=False 参数,我会得到一个不同的异常,即“requests.exceptions.SSLError: Can't connect to HTTPS URL because the SSL module is not available”。这似乎是一个可能的根本原因,虽然我不知道他们为什么会更改错误代码,但我找不到对 ssl 模块的任何引用,并且我验证了 certifi 已安装。有趣的是,如果我在 windows 中去掉 verify 参数,我会得到一个不同的异常,“requests.exceptions.SSLError: [Errno 1] _ssl.c:503: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed”
编辑:
提到的所有案例/场景的追溯
完整代码如上图:
Traceback(most recent call last):
File "testRequests.py", line 15, in <module>
r = requests.get(url, auth=('uid','passwd'), verify=False)
File "build/bdist.linux-x86_64/egg/requests/api.py", line 52, in get
File "build/bdist.linux-x86_64/egg/requests/api.py", line 40, in request
File "build/bdist.linux-x86_64/egg/requests/sessions.py", line 208, in request
File "build/bdist.linux-x86_64/egg/requests/models.py", line 586, in send
requests.exceptions.Timeout: Request timed out
如上所示的代码减去“verify=False”参数:
Traceback(most recent call last):
File "testRequests.py", line 15, in <module>
r = requests.get(url, auth=('uid','passwd'))
File "build/bdist.linux-x86_64/egg/requests/api.py", line 52, in get
File "build/bdist.linux-x86_64/egg/requests/api.py", line 40, in request
File "build/bdist.linux-x86_64/egg/requests/sessions.py", line 208, in request
File "build/bdist.linux-x86_64/egg/requests/models.py", line 584, in send
requests.exceptions.SSLError: Can't connect to HTTPS URL because the SSL module is not available
如上所示的代码减去“verify=False”参数并在windows下运行:
Traceback(most recent call last):
File "testRequests.py", line 59, in <module>
r = requests.get(url, auth=('uid','passwd'))
File "c:\Python27\lib\site-packages\requests\api.py", line 52, in get
return request('get', url, **kwargs)
File "c:\Python27\lib\site-packages\requests\api.py", line 40, in request
return s.request(method=method, url=url, **kwargs)
File "c:\Python27\lib\site-packages\requests\sessions.py", line 208, in request
r.send(prefetch=prefetch)
File "c:\Python27\lib\site-packages\requests\models.py", line 584, in send
raise SSLError(e)
requests.exceptions.SSLError: [Errno 1] _ssl.c:503: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed