python请求超时。获取整个响应


问题内容

我正在收集网站列表上的统计信息,为了简化起见,我使用了请求。这是我的代码:

data=[]
websites=['http://google.com', 'http://bbc.co.uk']
for w in websites:
    r= requests.get(w, verify=False)
    data.append( (r.url, len(r.content), r.elapsed.total_seconds(), str([(l.status_code, l.url) for l in r.history]), str(r.headers.items()), str(r.cookies.items())) )

现在,我想requests.get在10秒后超时,以免循环陷入困境。

这个问题以前也很有趣但是没有一个答案是正确的。我会为此悬赏,以得到一个不错的答案。

我听说也许不使用请求是一个好主意,但是我应该如何获得请求所提供的好处。(元组中的)


问题答案:

那如何使用eventlet?如果您想在10秒后使请求超时,即使正在接收数据,此代码段也将为您工作:

import requests
import eventlet
eventlet.monkey_patch()

with eventlet.Timeout(10):
    requests.get("http://ipv4.download.thinkbroadband.com/1GB.zip", verify=False)