使用这个 curl 命令,我可以从 Bash 获得我正在寻找的响应
curl -v -u z:secret_key --proxy http://proxy.net:80 \
-H "Content-Type: application/json" https://service.com/data.json
它帮助我用 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:
Requests 是如此优雅和令人敬畏,但在这种情况下它怎么会失败呢?