我的原始任务:使用Trello API,通过HTTP GET请求获取数据。如果可能,以异步方式运行请求和处理响应。 API提供程序使用我访问的“https://”URL和一些密钥和令牌。
我使用的工具:
requests
图书馆(只是
导入而无需安装)grequests
库(通过pip安装
来自this git repo) 原始任务结果:只有requests
库工作,我有Trello API的响应,非常好。 grequests
库失败,status_code = 302。
我试图了解它为什么会发生并编写两个可重现的脚本。
使用的脚本A :requests
库:
import requests
urls = [
"https://www.google.com",
"https://www.facebook.com/",
"http://www.facebook.com/",
"http://www.google.com",
"http://fakedomain/",
"http://python-tablib.org"
]
# Run requests:
for url in urls:
print requests.get(url).status_code
控制台输出A (由于http://fakedomain/
而有一些例外):
200
200
200
200
Traceback (most recent call last):
File "req.py", line 14, in <module>
print requests.get(url).status_code
File "D:\python\lib\site-packages\requests\api.py", line 69, in get
return request('get', url, params=params, **kwargs)
File "D:\python\lib\site-packages\requests\api.py", line 50, in request
response = session.request(method=method, url=url, **kwargs)
File "D:\python\lib\site-packages\requests\sessions.py", line 465, in request
resp = self.send(prep, **send_kwargs)
File "D:\python\lib\site-packages\requests\sessions.py", line 573, in send
r = adapter.send(request, **kwargs)
File "D:\python\lib\site-packages\requests\adapters.py", line 415, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', gaierror(11001, 'getaddrinfo failed'))
脚本B :grequests
库与map
一起发送异步请求:
import grequests
# This function will execute set of instructions when responses come:
def proc_response(response, **kwargs):
# do something ..
print response
# Request exception handler:
def my_except_handler(request, excetion):
print "Request failed : " + request.url
urls = [
"https://www.google.com",
"https://www.facebook.com/",
"http://www.facebook.com/",
"http://www.google.com",
"http://fakedomain/",
"http://python-tablib.org"
]
# Here is the list of tasks we build and run in parallel later:
actions_list = []
# Tasks list building:
for url in urls:
action_item = grequests.get(url, hooks = {'response' : proc_response})
actions_list.append(action_item)
# Run grequests:
print grequests.map(actions_list, exception_handler=my_except_handler)
控制台输出B :
<Response [302]>
<Response [302]>
<Response [200]>
<Response [301]>
<Response [302]>
<Response [200]>
Request failed : https://www.google.com
Request failed : https://www.facebook.com/
Request failed : http://www.facebook.com/
Request failed : http://fakedomain/
[None, None, None, <Response [200]>, None, <Response [200]>]
我可以根据这些信息得出结论,而我的相对较小的经验如下 - 由于某种原因,grequests
被远程网站requests
拒绝正常工作。只要302表示某种重定向,似乎grequests
无法从源获取数据,而requests
可以将其重定向到。 1}}在脚本B的allow_redirects=True
方法中没有解决问题。
我想知道为什么图书馆会给出不同的答案。我可能会错过一些东西,这两个脚本必须通过设计返回不同的结果,而不是因为两个库之间存在差异。
提前感谢您的帮助。
答案 0 :(得分:3)
这是我的脚本b.py
,我通过$ py.test -sv b.py
运行:
import pytest
import grequests
@pytest.fixture
def urls():
return [
"https://www.google.com",
"https://www.facebook.com/",
"http://www.facebook.com/",
"http://www.google.com",
"http://fakedomain/",
"http://python-tablib.org"
]
# This function will execute set of instructions when responses come:
def proc_response(response, **kwargs):
# do something ..
print "========Processing response=============", response.request.url
print response
if response.status_code != 200:
print response.request.url
print response.content
# Request exception handler:
def my_except_handler(request, exception):
print "Request failed : " + request.url
print request.response
def test_it(urls):
# Here is the list of tasks we build and run in parallel later:
actions_list = []
# Tasks list building:
for url in urls:
action_item = grequests.get(url, hooks={'response': proc_response})
actions_list.append(action_item)
# Run grequests:
print grequests.map(actions_list, exception_handler=my_except_handler)
它基于您的代码,只是为了方便我的实验而重写。
我测试的最后一次打印显示:
[<Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, None, <Response [200]>]
这是预期的。
请注意,获取数据可能会遇到一些临时问题,因此玩家太多了 参与。
不同之处在于,requests
与grequests
process_response
时要求最终结果
部署requests
挂钩,为每个响应调用,包括重定向响应。
modprobe net_dev hw_enable=1
处理也会重定向,但不会报告此临时响应。