util.py 文件源码

python
阅读 20 收藏 0 点赞 0 评论 0

项目:cget 作者: pfultz2 项目源码 文件源码
def download_to(url, download_dir, insecure=False):
    name = url.split('/')[-1]
    file = os.path.join(download_dir, name)
    click.echo("Downloading {0}".format(url))
    bar_len = 1000
    with click.progressbar(length=bar_len, width=70) as bar:
        def hook(count, block_size, total_size):
            percent = int(count*block_size*bar_len/total_size)
            if percent > 0 and percent < bar_len: 
                # Hack because we can't set the position
                bar.pos = percent
                bar.update(0)
        context = None
        if insecure: context = ssl._create_unverified_context()
        CGetURLOpener(context=context).retrieve(url, filename=file, reporthook=hook, data=None)
        bar.update(bar_len)
    if not os.path.exists(file):
        raise BuildError("Download failed for: {0}".format(url))
    return file
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号