当前位置:  编程技术>python

python支持断点续传的多线程下载示例

    来源: 互联网  发布时间:2014-09-04

    本文导语:  代码如下:#! /usr/bin/env python#coding=utf-8 from __future__ import unicode_literals from multiprocessing.dummy import Pool as ThreadPoolimport threading import osimport sysimport cPicklefrom collections import namedtupleimport urllib2from urlparse import urlsplit import time # global locklo...

代码如下:

#! /usr/bin/env python
#coding=utf-8

from __future__ import unicode_literals

from multiprocessing.dummy import Pool as ThreadPool
import threading

import os
import sys
import cPickle
from collections import namedtuple
import urllib2
from urlparse import urlsplit

import time


# global lock
lock = threading.Lock()


# default parameters
defaults = dict(thread_count=10,
    buffer_size=10*1024,
    block_size=1000*1024)


def progress(percent, width=50):
    print "%s %d%%r" % (('%%-%ds' % width) % (width * percent / 100 * '='), percent),
    if percent >= 100:
        print
        sys.stdout.flush()


def write_data(filepath, data):
    with open(filepath, 'wb') as output:
        cPickle.dump(data, output)


def read_data(filepath):
    with open(filepath, 'rb') as output:
        return cPickle.load(output)


FileInfo = namedtuple('FileInfo', 'url name size lastmodified')


def get_file_info(url):
    class HeadRequest(urllib2.Request):
        def get_method(self):
            return "HEAD"
    res = urllib2.urlopen(HeadRequest(url))
    res.read()
    headers = dict(res.headers)
    size = int(headers.get('content-length', 0))
    lastmodified = headers.get('last-modified', '')
    name = None
    if headers.has_key('content-disposition'):
        name = headers['content-disposition'].split('filename=')[1]
        if name[0] == '"' or name[0] == "'":
            name = name[1:-1]
    else:
        name = os.path.basename(urlsplit(url)[2])

    return FileInfo(url, name, size, lastmodified)


def download(url, output,
        thread_count = defaults['thread_count'],
        buffer_size = defaults['buffer_size'],
        block_size = defaults['block_size']):
    # get latest file info
    file_info = get_file_info(url)

    # init path
    if output is None:
        output = file_info.name
    workpath = '%s.ing' % output
    infopath = '%s.inf' % output

    # split file to blocks. every block is a array [start, offset, end],
    # then each greenlet download filepart according to a block, and
    # update the block' offset.
    blocks = []

    if os.path.exists(infopath):
        # load blocks
        _x, blocks = read_data(infopath)
        if (_x.url != url or
                _x.name != file_info.name or
                _x.lastmodified != file_info.lastmodified):
            blocks = []

    if len(blocks) == 0:
        # set blocks
        if block_size > file_info.size:
            blocks = [[0, 0, file_info.size]]
        else:
            block_count, remain = divmod(file_info.size, block_size)
            blocks = [[i*block_size, i*block_size, (i+1)*block_size-1] for i in range(block_count)]
            blocks[-1][-1] += remain
        # create new blank workpath
        with open(workpath, 'wb') as fobj:
            fobj.write('')

    print 'Downloading %s' % url
    # start monitor
    threading.Thread(target=_monitor, args=(infopath, file_info, blocks)).start()

    # start downloading
    with open(workpath, 'rb+') as fobj:
        args = [(url, blocks[i], fobj, buffer_size) for i in range(len(blocks)) if blocks[i][1] < blocks[i][2]]

        if thread_count > len(args):
            thread_count = len(args)

        pool = ThreadPool(thread_count)
        pool.map(_worker, args)
        pool.close()
        pool.join()


    # rename workpath to output
    if os.path.exists(output):
        os.remove(output)
    os.rename(workpath, output)

    # delete infopath
    if os.path.exists(infopath):
        os.remove(infopath)

    assert all([block[1]>=block[2] for block in blocks]) is True


def _worker((url, block, fobj, buffer_size)):
    req = urllib2.Request(url)
    req.headers['Range'] = 'bytes=%s-%s' % (block[1], block[2])
    res = urllib2.urlopen(req)

    while 1:
        chunk = res.read(buffer_size)
        if not chunk:
            break
        with lock:
            fobj.seek(block[1])
            fobj.write(chunk)
            block[1] += len(chunk)


def _monitor(infopath, file_info, blocks):
    while 1:
        with lock:
            percent = sum([block[1] - block[0] for block in blocks]) * 100 / file_info.size
            progress(percent)
            if percent >= 100:
                break
            write_data(infopath, (file_info, blocks))
        time.sleep(2)


if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser(description='Download file by multi-threads.')
    parser.add_argument('url', type=str, help='url of the download file')
    parser.add_argument('-o', type=str, default=None, dest="output", help='output file')
    parser.add_argument('-t', type=int, default=defaults['thread_count'], dest="thread_count", help='thread counts to downloading')
    parser.add_argument('-b', type=int, default=defaults['buffer_size'], dest="buffer_size", help='buffer size')
    parser.add_argument('-s', type=int, default=defaults['block_size'], dest="block_size", help='block size')

    argv = sys.argv[1:]

    if len(argv) == 0:
        argv = ['https://eyes.nasa.gov/eyesproduct/EYES/os/win']

    args = parser.parse_args(argv)

    start_time = time.time()
    download(args.url, args.output, args.thread_count, args.buffer_size, args.block_size)
    print 'times: %ds' % int(time.time()-start_time)


    
 
 

您可能感兴趣的文章:

  • Python获取网页编码的方法及示例代码
  • python读取csv文件示例(python操作csv)
  • python单线程实现多个定时器示例 iis7站长之家
  • python基础教程之python消息摘要算法使用示例
  • 数据结构:图(有向图,无向图),在Python中的表示和实现代码示例
  • python实现绘制树枝简单示例
  • 使用python删除nginx缓存文件示例(python文件操作)
  • python学习手册中的python多态示例代码
  • python调用windows api锁定计算机示例
  • python代码制作configure文件示例
  • python使用循环实现批量创建文件夹示例
  • python采用requests库模拟登录和抓取数据的简单示例
  • Python数组条件过滤filter函数使用示例
  • python获得图片base64编码示例
  • Python的print用法示例
  • python文件读写并使用mysql批量插入示例分享(python操作mysql)
  • Python pass 语句使用示例
  • python getopt 参数处理小示例
  • python的urllib模块显示下载进度示例
  • python list转dict示例分享
  • python求素数示例分享
  •  
    本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
    本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • Python线程框架 greenlet
  • python 多线程应用介绍
  • Python中多线程thread与threading的实现方法
  • python线程锁(thread)学习示例
  • python线程池的实现实例
  • python使用urllib模块开发的多线程豆瓣小站mp3下载器
  • python多线程编程方式分析示例详解
  • python多线程http下载实现示例
  • python多线程扫描端口示例
  • Python中用Ctrl+C终止多线程程序的问题解决
  • python中的多线程实例教程
  • Python实现多线程下载文件的代码实例
  • python实现多线程采集的2个代码例子
  • 理解python多线程(python多线程简明教程)
  • Python代理抓取并验证使用多线程实现
  • python多线程抓取天涯帖子内容示例
  • python单线程实现多个定时器示例
  • Python多线程学习资料
  • Python使用代理抓取网站图片(多线程)
  • Python GUI编程:tkinter实现一个窗口并居中代码
  • 让python同时兼容python2和python3的8个技巧分享
  • Python不使用print而直接输出二进制字符串
  • 使用setup.py安装python包和卸载python包的方法
  • Python中实现json字符串和dict类型的互转
  • 不小心把linux自带的python卸载了,导致安装一个依赖原python的软件不能安装,请问该怎么办?
  • python异常信息堆栈输出到日志文件
  • Python开发者社区整站源码 Pythoner
  • python下用os.execl执行centos下的系统时间同步命令ntpdate
  • 新手该如何学python怎么学好python?
  • Python namedtuple对象json序列化/反序列化及对象恢复
  • 请教:system("C:\python2.4\python.exe C:\aa.py");该语句有何错误?为什么运行界面一闪就消失了并且没有运行完,请给出正确语句!


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3