使用这个curl命令,我可以从Bash获得所需的响应
curl -v -u z:secret_key --proxy http://proxy.net:80 \
-H "Content-Type: application/json" https://service.com/data.json
I have already seen this other post on proxies with the Requests module
它帮助我用Python编写了代码,但是我需要通过代理进行请求。但是,即使提供了适当的代理,它也无法正常工作。也许我只是没看到什么?
>>> requests.request('GET', 'https://service.com/data.json', \
>>> headers={'Content-Type':'application/json'}, \
>>> proxies = {'http' : "http://proxy.net:80",'https':'http://proxy.net:80'}, \
>>> auth=('z', 'secret_key'))
此外,在同一个python控制台上,我可以使用urllib发出成功的请求。
>>> import urllib
>>> urllib.urlopen("http://www.httpbin.org").read()
---results---
即使仅在非https地址上尝试请求也无法正常工作。
>>> requests.get('http://www.httpbin.org')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Python/2.6/site-packages/requests/api.py", line 79, in get
return request('get', url, **kwargs)
File "/Library/Python/2.6/site-packages/requests/api.py", line 66, in request
prefetch=prefetch
File "/Library/Python/2.6/site-packages/requests/sessions.py", line 191, in request
r.send(prefetch=prefetch)
File "/Library/Python/2.6/site-packages/requests/models.py", line 454, in send
raise ConnectionError(e)
requests.exceptions.ConnectionError: Max retries exceeded for url:
请求是如此优雅和令人敬畏,但是在这种情况下它怎么会失败?
最佳答案
问题实际上出在python的标准URL访问库-urllib/urllib2/httplib。我不记得哪个库是真正的罪魁祸首,但是为了简单起见,我们仅将其称为urllib。不幸的是,urllib没有实现HTTP连接方法,而HTTP连接方法是通过http(s)代理访问https站点所必需的。我使用urllib添加功能的努力并未成功(自尝试以来已有一段时间)。因此,不幸的是,我知道可以使用的唯一方法是在这种情况下使用pycurl。
但是,有一个相对干净的解决方案,它与python请求几乎完全相同,但使用的是pycurl后端而不是python标准库。
该库称为human_curl。我自己使用过,并取得了很好的效果。
关于python - 通过Python Requests模块发出的HTTP请求在curl不能通过代理工作的地方?为什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8482896/