小编ntl*_*son的帖子

Python请求库在Linux下超时

我试图使用requests.py库来调用其余的Web服务.我在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)
Run Code Online (Sandbox Code Playgroud)

我注意到如果我从get调用中省略了verify = False参数,我会得到一个不同的异常,即"requests.exceptions.SSLError:无法连接到HTTPS URL,因为SSL模块不可用".这似乎是一个可能的根本原因,虽然我不知道他们为什么会改变错误代码,但我找不到任何对ssl模块的引用,我验证了certifi已经安装.有趣的是,如果我在Windows中省略了验证参数,我会得到一个不同的异常,"requests.exceptions.SSLError:[Errno 1] _ssl.c:503:错误:14090086:SSL例程:SSL3_GET_SERVER_CERTIFICATE:证书验证失败"

编辑:

所提到的所有案例/场景的回溯

完整代码如上图所示:

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
Run Code Online (Sandbox Code Playgroud)

如上所示的代码减去"verify = False"参数:

Traceback(most recent call last):
    File "testRequests.py", line 15, in <module> …
Run Code Online (Sandbox Code Playgroud)

python python-requests

5
推荐指数
1
解决办法
2924
查看次数

标签 统计

python ×1

python-requests ×1