我正在收集网站列表上的统计数据,为了简单起见,我正在使用请求。这是我的代码:
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())) )
现在,我想要请求。10秒后进入超时,这样循环就不会卡住。
这个问题以前也很有趣,但没有一个答案是干净的。
我听说可能不使用请求是一个好主意,但我应该如何得到请求提供的好东西(元组中的那些)。
嗯,我尝试了这个页面上的许多解决方案,仍然面临不稳定,随机挂起,连接性能差。
我现在正在使用Curl,我对它的“max time”功能和全局性能非常满意,即使实现如此糟糕:
content=commands.getoutput('curl -m6 -Ss "http://mywebsite.xyz"')
这里,我定义了一个最大6秒的时间参数,包括连接时间和传输时间。
我相信Curl有一个很好的python绑定,如果你更喜欢坚持python语法:)
我使用请求2.2.1和eventlet不适合我。相反,我可以使用gevent超时代替,因为gevent在我的服务中用于gunicorn。
import gevent
import gevent.monkey
gevent.monkey.patch_all(subprocess=True)
try:
with gevent.Timeout(5):
ret = requests.get(url)
print ret.status_code, ret.content
except gevent.timeout.Timeout as e:
print "timeout: {}".format(e.message)
请注意geevent .timeout. timeout不会被常规异常处理捕获。
所以要么显式地捕获getevent。timeout。timeout
或者传入一个不同的异常,像这样使用:with gevent。Timeout(5, requests.exceptions.Timeout):尽管在引发此异常时没有传递任何消息。
只是另一个解决方案(从http://docs.python-requests.org/en/master/user/advanced/#streaming-uploads获得)
在上传之前,你可以找出内容大小:
TOO_LONG = 10*1024*1024 # 10 Mb
big_url = "http://ipv4.download.thinkbroadband.com/1GB.zip"
r = requests.get(big_url, stream=True)
print (r.headers['content-length'])
# 1073741824
if int(r.headers['content-length']) < TOO_LONG:
# upload content:
content = r.content
但是要小心,发送方可以在“content-length”响应字段中设置不正确的值。
设置stream=True并使用r.iter_content(1024)。是的,eventlet。我就是不喜欢超时。
try:
start = time()
timeout = 5
with get(config['source']['online'], stream=True, timeout=timeout) as r:
r.raise_for_status()
content = bytes()
content_gen = r.iter_content(1024)
while True:
if time()-start > timeout:
raise TimeoutError('Time out! ({} seconds)'.format(timeout))
try:
content += next(content_gen)
except StopIteration:
break
data = content.decode().split('\n')
if len(data) in [0, 1]:
raise ValueError('Bad requests data')
except (exceptions.RequestException, ValueError, IndexError, KeyboardInterrupt,
TimeoutError) as e:
print(e)
with open(config['source']['local']) as f:
data = [line.strip() for line in f.readlines()]
讨论在这里https://redd.it/80kp1h