6

我正在尝试使用 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
4

1 回答 1

0

我不是这方面的专家,但似乎无法正确验证来自服务器的证书。我不知道 Python 和 ssl 如何处理证书验证,但第一个选项是尝试忽略异常,或者更改httpshttp尝试查看 Web 服务是否允许非安全服务调用。

如果问题是围绕ssl. 考虑删除python的包(小心)并使用openssl支持编译它,我个人强烈建议你在删除任何东西之前先查看一个virtualenv,编译Python并不太难,它会给你一个更精细的控制你的目标是做。

于 2012-04-17T20:03:08.430 回答