如何限制请求python库中HTTP请求的下载速率? [英] How to limit download rate of HTTP requests in requests python library?
问题描述
是否可以使用 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屋!