使用 urllib2 进行节流
使用 urllib2 时是否可以轻松限制 kbps? 如果是的话,您可以指导我找到任何代码示例或资源,我将不胜感激。
is it possible to easily cap the kbps when using urllib2
?
If it is, any code examples or resources you could direct me to would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
urllib
模块中有urlretrieve(url, filename=None, reporthook=None, data=None)
函数。如果您将
reporthook
-function/object 实现为 令牌桶,或者漏桶,你有你的全局速率限制。编辑:经过仔细检查,我发现使用
reporthook
进行全局速率限制并不像我想象的那么容易。reporthook
仅给出下载量和总大小,其本身不足以提供与令牌桶一起使用的信息。 解决这个问题的一种方法是将最后下载的数量存储在每个速率限制器中,但使用全局令牌桶。编辑2:将两个代码合并到一个示例中。
There is the
urlretrieve(url, filename=None, reporthook=None, data=None)
function in theurllib
module.If you implement the
reporthook
-function/object as either a token bucket, or a leaky bucket, you have your global rate-limit.EDIT: Upon closer examination I see that it isn't as easy to do global rate-limit with
reporthook
as I thought.reporthook
is only given the downloaded amount and the total size, which on their own isn't enough to information to use with the token-bucket. One way to get around it is by storing the last downloaded amount in each rate-limiter, but use a global token-bucket.EDIT 2: Combined both codes into one example.