python 请求超时.获取整个响应 [英] Timeout for python requests.get entire response

查看:53
本文介绍了python 请求超时.获取整个响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在收集有关网站列表的统计数据,并且为了简单起见,我正在使用它的请求.这是我的代码:

I'm gathering statistics on a list of websites and I'm using requests for it for simplicity. Here is my code:

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 秒后超时,这样循环就不会卡住.

Now, I want requests.get to timeout after 10 seconds so the loop doesn't get stuck.

这个问题之前一直很有趣 也是,但没有一个答案是干净的.我会为此付出一些代价以获得一个好的答案.

This question has been of interest before too but none of the answers are clean. I will be putting some bounty on this to get a nice answer.

我听说不使用请求可能是个好主意,但是我应该如何获得请求提供的好东西.(元组中的那些)

I hear that maybe not using requests is a good idea but then how should I get the nice things requests offer. (the ones in the tuple)

推荐答案

使用 eventlet 怎么样?如果您想在 10 秒后超时请求,即使正在接收数据,此代码段也适用于您:

What about using eventlet? If you want to timeout the request after 10 seconds, even if data is being received, this snippet will work for you:

import requests
import eventlet
eventlet.monkey_patch()

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

这篇关于python 请求超时.获取整个响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆