如何在请求python库中限制HTTP请求的下载速率? [英] How to limit download rate of HTTP requests in requests python library?

查看:777
本文介绍了如何在请求python库中限制HTTP请求的下载速率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 requests Python库限制GET请求的下载速率?例如,使用如下命令:

Is it possible to limit the download rate of GET requests using the requests Python library? For instance, with a command like this:

r = requests.get('https://stackoverflow.com/')

...是否可以限制下载速率?我希望类似于这个 wget 命令:

...is it possible to limit the download rate? I'm hoping for something similar to this wget command:

wget --limit-rate=20k https://stackoverflow.com/

我知道它是可能 urllib2 。我具体询问 requests 库。

I know it's possible with urllib2. I'm asking specifically about the requests library.

推荐答案

几种速率限制方法;其中一个是令牌桶,你可以找到这里的食谱另一个在这里

There are several approaches to rate limiting; one of them is token bucket, for which you can find a recipe here and another one here.

通常你会想做限制或 socket.send() socket.recv()的速率限制。您可以使用 socket-throttle 并查看它是否符合您的需要。

Usually you would want to do throttling or rate limiting on socket.send() and socket.recv(). You could play with socket-throttle and see if it does what you need.

这不应与 x-ratelimit 限速响应标题,与许多请求相关,而不是下载/传输速率。

This is not to be confused with x-ratelimit rate limiting response headers, which are related to a number of requests rather than a download / transfer rate.

这篇关于如何在请求python库中限制HTTP请求的下载速率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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