当前位置:编程学习 > 网站相关 >>

urllib 如何限速下载

 

#!/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()   

补充:Web开发 , Python ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,