我试图获得一个使用requests
模块通过代理请求数据的Python脚本。根据{{3}}:
您还可以通过设置环境变量HTTP_PROXY和HTTPS_PROXY来配置代理。
但是,据我所知,这仅适用于您的目标URL引用主机名,并且在目标URL引用IP地址时失败。例如,我正在尝试获取资源http://overcloud-public-vip:5000
。主机名(overcloud-public-vip
)无法在本地解析,但可由代理解析为192.168.30.6
。这有效:
>>> import os
>>> import requests
>>> os.environ['HTTP_PROXY'] = 'http://localhost:8123'
>>> requests.get('http://overcloud-public-vip:5000')
<Response [300]>
但是,如果我尝试完全相同的事情,使用IP地址而不是 主机名,它失败了:
>>> import os
>>> import requests
>>> os.environ['HTTP_PROXY'] = 'http://localhost:8123'
>>> requests.get('http://192.168.30.6:5000')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/my/project/env/lib/python2.7/site-packages/requests/api.py", line 67, in get
return request('get', url, params=params, **kwargs)
File "/my/project/env/lib/python2.7/site-packages/requests/api.py", line 53, in request
return session.request(method=method, url=url, **kwargs)
File "/my/project/env/lib/python2.7/site-packages/requests/sessions.py", line 468, in request
resp = self.send(prep, **send_kwargs)
File "/my/project/env/lib/python2.7/site-packages/requests/sessions.py", line 576, in send
r = adapter.send(request, **kwargs)
File "/my/project/env/lib/python2.7/site-packages/requests/adapters.py", line 437, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='192.168.30.6', port=5000): Max retries exceeded with url: / (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x7f89e7d52950>: Failed to establish a new connection: [Errno 110] Connection timed out',))
我可以确认(使用strace -e trace=connect python
)该模块
在这一秒内永远不会尝试联系代理服务器
情况下。
这特别令人沮丧,因为即使我使用了传递URL 一个主机名,代码将最终从中检索服务目录 本身包含IP地址的远程服务器,所以我很简单 将失败推迟了一步。
这种行为似乎与文档不一致。我正在使用 请求版本2.9.1:
>>> requests.__version__
'2.9.1'
这是requests
中的错误,还是我脑中的错误?