频道栏目
首页 > 资讯 > Python > 正文

urllib 如何限速下载

11-07-30        来源:[db:作者]  
收藏   我要投稿

 

#!/usr/bin/python 

#coding:utf-8

 

 

""" 

urllib 如何限速下载

file:ratelimit-urllib.py

Fetch a url rate limited 

 

Syntax: rate URL local_file_name 

 

python ratelimit-urllib.py 200 http://localhost/share/netbeans-7.0-ml-linux.sh  1.sh

 

""" 

 

import os 

import sys 

import urllib 

from time import time, sleep 

 

class RateLimit(object): 

    """Rate limit a url fetch""" 

    def __init__(self, rate_limit): 

        """rate limit in kBytes / second""" 

        self.rate_limit = rate_limit 

        self.start = time() 

    def __call__(self, block_count, block_size, total_size): 

        total_kb = total_size / 1024 

        downloaded_kb = (block_count * block_size) / 1024 

        elapsed_time = time() - self.start 

        if elapsed_time != 0: 

            #实际下载速度

            rate = downloaded_kb / elapsed_time 

            print "%d kb of %d kb downloaded %f.1 kBytes/s\n" % (downloaded_kb ,total_kb, rate), 

            expected_time = downloaded_kb / self.rate_limit 

            sleep_time = expected_time - elapsed_time 

            print "Sleep for", sleep_time 

            #等待时间

            if sleep_time > 0: 

                sleep(sleep_time) 

 

def main(): 

    """Fetch the contents of urls""" 

    if len(sys.argv) != 4: 

        print 'Syntax: %s "rate in kBytes/s" URL "local output path"' % sys.argv[0] 

        raise SystemExit(1) 

    rate_limit, url, out_path = sys.argv[1:] 

    rate_limit = float(rate_limit) 

    print "Fetching %r to %r with rate limit %.1f" % (url, out_path, rate_limit) 

    #这个扩展接口支持一个回调函数,每下载一段就调用一次回调函数

    urllib.urlretrieve(url, out_path, reporthook=RateLimit(rate_limit)) 

 

if __name__ == "__main__": main()   

相关TAG标签
上一篇:python 对一个函数执行速度控制的演示
下一篇:Oracle认证:数据仓库离我们到底有多远?
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站