python多进程断点续传分片下载器

标签:python 下载器 多进程


因为爬虫要用到下载器,但是直接用urllib下载很慢,所以找了很久终于找到一个让我欣喜的下载器。他能够断点续传分片下载,极大提高下载速度。

#! /usr/bin/env python
# encoding=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=500 * 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='多线程文件下载器.')
parser.add_argument('url', type=str, help='下载连接')
parser.add_argument('-o', type=str, default=None,
dest="output", help='输出文件')
parser.add_argument(
'-t', type=int, default=defaults['thread_count'], dest="thread_count", help='下载的线程数量')
parser.add_argument(
'-b', type=int, default=defaults['buffer_size'], dest="buffer_size", help='缓存大小')
parser.add_argument(
'-s', type=int, default=defaults['block_size'], dest="block_size", help='字区大小') 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 '下载时间: %ds' % int(time.time() - start_time)

python多进程断点续传分片下载器的更多相关文章

  1. Python实现多线程HTTP下载器

    本文将介绍使用Python编写多线程HTTP下载器,并生成.exe可执行文件. 环境:windows/Linux + Python2.7.x 单线程 在介绍多线程之前首先介绍单线程.编写单线程的思路为 ...

  2. 用python做youtube自动化下载器 代码

    目录 项目地址 思路 流程 1. post i. 先把post中的headers格式化 ii.然后把参数也格式化 iii. 最后再执行requests库的post请求 iv. 封装成一个函数 2. 调 ...

  3. python的内置下载器

    python有个内置下载器,有时候在内部提供文件下载很好用. 进入提供下载的目录 # ls abc.aaa chpw.py finance.py lsdir.py ping.py u2d-partia ...

  4. Qt+Python开发百度图片下载器

    一.资源下载地址 https://www.aliyundrive.com/s/jBU2wBS8poH 本项目路径:项目->收费->百度图片下载器(可试用5分钟) 安装包直接下载地址:htt ...

  5. 用python做youtube自动化下载器 思路

    目录 0. 思路 1.准备 i.savfrom.net 2. 探索并规划获取方式 i.总览 ii. 获取该网页取到下载url的请求 iii. 在本地获取请求 iv.解析请求结果 v.解析解密后的结果 ...

  6. Python 多进程 一分钟下载二百张图片 是什么样子的体验

    需要爬取国内某个网站,但是这个网站封ip,没办法,只能用代理了,然后构建自己的代理池,代理池维护了20条进程, 所用的网络是20M带宽,实际的网速能达到2.5M,考虑到其他原因,网速未必能达到那么多. ...

  7. Android通用简洁的下载器

    下载逻辑在android开发中可谓很常见,那么封装一个通用简洁的下载器时很有必要的.如果不想给工程引入一个很重的jar包那么可以直接复用下面的代码即可. 主要对外接口 构造函数 :     publi ...

  8. 基于iOS 10、realm封装的下载器

    代码地址如下:http://www.demodashi.com/demo/11653.html 概要 在决定自己封装一个下载器前,我本以为没有那么复杂,可在实际开发过程中困难重重,再加上iOS10和X ...

  9. 用python实现的百度音乐下载器-python-pyqt-改进版

    之前写过一个用python实现的百度新歌榜.热歌榜下载器的博文,实现了百度新歌.热门歌曲的爬取与下载.但那个采用的是单线程,网络状况一般的情况下,扫描前100首歌的时间大概得到40来秒.而且用Pyqt ...

随机推荐

  1. HTML&CSS基础学习笔记1.20-DIV标签2

    <span>标签 标签div把文档分割为独立的.不同的部分,而在HTML中,<span>标签则被用来组合文档中的行内元素. 看个实例: <!DOCTYPE html> ...

  2. python修改txt文件内容

    ①以r模式打开文件并用readlines方法读入列表l中 ②修改相关行,直接用l[n]形式即可 ③关闭文件 ④以w方式打开文件,用writelines方法写入文件(覆盖文件内容) ⑤关闭文件 需要注意 ...

  3. Pick two points at random from the interior of a unit square, what is the expected distance between them?

    My solution is as folllowing. This integration is hard to solve. I googled it, and found the result ...

  4. write a macro to judge big endian or little endian

    Big endian means the most significant byte stores first in memory. int a=0x01020304, if the cpu is b ...

  5. UART与USART的区别

    UART与USART都是单片机上的串口通信,他们之间的区别如下: 首先从名字上看: UART:universal asynchronous receiver and transmitter通用异步收/ ...

  6. IIC总线协议---以存储芯片at24c64为例

    IIC总线协议 前言:年前给老师做个红外抄表系统,,现在对当中用到的一些模块总结一下. 1.只有在总线空闲时才允许启动数据传送. 2.在数据传送过程中,当时钟线为高电平时,数据线必须保持稳定状态,不允 ...

  7. centos下httpd 启动失败的解决办法

    [root@csit yang]# service httpd start Starting httpd:                                            [FA ...

  8. BZOJ1123: [POI2008]BLO

    1123: [POI2008]BLO Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 614  Solved: 235[Submit][Status] ...

  9. android真机调试

    android开发可以使用google那个自带的模拟器来调试,不过那个模拟器启动实在太慢,太耗时了,不过,如果我们有android手机的话,我们可以直接在手机上调试,这样的话,速度就很快: 具体步骤如 ...

  10. glibc的了解,对内核的封装

    glibc除了提供最底层的C运行库,还封装了kernel提供的API,程序通过glibc进行系统调用( syscall). 应用层面的C库,比如OpenSSL库: /usr/include/opens ...