python多线程下载
# -*- coding=utf-8 -*-
import sys
import os
import os.path
import time
import urllib.request, urllib.parse, urllib.error
from threading import Thread local_proxies = {} class ThreadDownComplete:
def __init__(self, down_loaded):
self.down_loaded = down_loaded class ThreadDown(Thread, urllib.request.FancyURLopener):
def __init__(self, thread_name, url, filename, ranges=0, proxies={}):
Thread.__init__(self, name=thread_name)
urllib.request.FancyURLopener.__init__(self, proxies)
self.name = thread_name
self.url = url
self.filename = filename
self.ranges = ranges
self.down_loaded = 0
self.url_handler = None
self.one_time_size = 16384 # 16kByte/time
self.data_start_point = self.ranges[0]
self.data_end_point = self.ranges[1]
self.down_start_time = int(time.time())
self.recent_down_time = int(time.time())
self.stop = False
self.complete = False
self.download_loan = self.data_end_point - self.data_start_point
try:
self.down_loaded = os.path.getsize(self.filename)
except OSError:
self.down_loaded = 0
self.start_point = self.data_start_point + self.down_loaded if not self.complete_check():
self.data_check() def start_reset(self):
try:
os.remove(self.filename)
except:
pass self.down_loaded = 0
self.complete = False
self.stop = False
self.start_point = self.data_start_point def data_check(self):
if self.start_point > self.data_end_point:
print("Part %s has been down_loaded over.\n" % self.filename)
self.start_reset() def flow_check(self):
if self.down_loaded > self.download_loan + 1:
self.stop = False
return False
return True def complete_check(self):
if self.down_loaded == self.download_loan + 1:
self.complete = True
self.stop = True
return True
return False def down(self):
try:
return self.url_handler.read(self.one_time_size)
except:
return '' def __run(self): print("task %s will down_load from %d to %d" % (self.name, self.start_point, self.data_end_point))
self.addheader("Range", "bytes=%d-%d" % (self.start_point, self.data_end_point))
self.url_handler = self.open(self.url)
data = self.down() while not self.stop and not self.complete:
if data:
self.recent_down_time = int(time.time())
file_handler = open(self.filename, 'ab+')
file_handler.write(data)
file_handler.close()
self.down_loaded += len(data) if self.complete_check():
break
if not self.flow_check():
break data = self.down() def run(self): if self.complete:
return self.__run()
self.complete_check()
while not self.stop and not self.complete:
self.start_reset()
self.__run()
self.complete_check() def get_file_size(url, proxies={}):
url_handler = urllib.request.urlopen(url)
return int(url_handler.info()['Content-Length']) def split_blocks(total_size, block_number):
block_size = int(total_size / block_number)
ranges = []
for i in range(0, block_number - 1):
ranges.append((i * block_size, (i + 1) * block_size - 1))
ranges.append(((block_number - 1) * block_size, total_size - 1))
return ranges def is_live(tasks):
for index, task in enumerate(tasks):
if isinstance(task, ThreadDownComplete):
continue
if int(time.time()) - task.recent_down_time > 8:
thread_name = task.name
filename = task.filename
ranges = task.ranges
url = task.url
task.stop = True
tasks[index] = start_down_thread(thread_name, url, filename, ranges)
return True
if task.complete:
tasks[index] = ThreadDownComplete(task.down_loaded)
if task.is_alive():
return True
return False def start_down_thread(thread_name, url, filename, ranges):
task = ThreadDown(thread_name, url, filename, ranges)
task.setDaemon(True)
task.start()
return task def log(msg):
sys.stdout.write(msg)
sys.stdout.flush() def down_load(url, output, blocks=6, proxies=local_proxies):
down_file_size = get_file_size(url, proxies)
ranges = split_blocks(down_file_size, blocks) thread_name = ["thread_%d" % i for i in range(0, blocks)]
filename = [output + "_%d" % i for i in range(0, blocks)] tasks = []
for i in range(0, blocks):
tasks.append(start_down_thread(thread_name[i], url, filename[i], ranges[i])) while is_live(tasks):
down_loaded = sum([task.down_loaded for task in tasks])
process = down_loaded / float(down_file_size) * 100
log("\rfilesize:%d down_loaded:%d Completed:%.2f%%" % (down_file_size, down_loaded, process))
time.sleep(0.01) file_handler = open(output, 'wb+')
for i in filename:
f = open(i, 'rb')
file_handler.write(f.read())
f.close()
try:
os.remove(i)
pass
except:
pass file_handler.close() if os.path.getsize(output) == down_file_size:
log("\nCompleted\n")
else:
log("\nError\n") sys.exit(0) if __name__ == '__main__':
_url = "http://dldir1.qq.com/qqfile/qq/QQ7.9Light/14308/QQ7.9Light.exe"
down_load(_url, os.path.basename(_url), blocks=30, proxies={})
python多线程下载的更多相关文章
- python多线程下载文件
从文件中读取图片url和名称,将url中的文件下载下来.文件中每一行包含一个url和文件名,用制表符隔开. 1.使用requests请求url并下载文件 def download(img_url, i ...
- python多线程下载ts文件
# -*- coding: utf-8 -*- """ Created on Wed Aug 22 15:56:19 2018 @author: Administrato ...
- python多线程下载网页图片并保存至特定目录
#!python3 #multidownloadXkcd.py - Download XKCD comics using multiple threads. import requests impor ...
- python线程使用场景 多线程下载
http://blog.xiayf.cn/2015/09/11/parallelism-in-one-line http://python.jobbole.com/84327/ http://www. ...
- PYTHON文件多线程下载
其实,在一般的文件编程中,这有两个概念要说明: 第一是,下载一个大文件,将这个大文件多为多线程. 第二是,下载N多小文件,将每个线程指定下载多个小文件. 现在实现的是多线程下载一个大文件. 今天完成了 ...
- Python之FTP多线程下载文件之分块多线程文件合并
Python之FTP多线程下载文件之分块多线程文件合并 欢迎大家阅读Python之FTP多线程下载系列之二:Python之FTP多线程下载文件之分块多线程文件合并,本系列的第一篇:Python之FTP ...
- Python之FTP多线程下载文件之多线程分块下载文件
Python之FTP多线程下载文件之多线程分块下载文件 Python中的ftplib模块用于对FTP的相关操作,常见的如下载,上传等.使用python从FTP下载较大的文件时,往往比较耗时,如何提高从 ...
- python爬取快手视频 多线程下载
就是为了兴趣才搞的这个,ok 废话不多说 直接开始. 环境: python 2.7 + win10 工具:fiddler postman 安卓模拟器 首先,打开fiddler,fiddler作为htt ...
- python多线程爬虫+批量下载斗图啦图片项目(关注、持续更新)
python多线程爬虫项目() 爬取目标:斗图啦(起始url:http://www.doutula.com/photo/list/?page=1) 爬取内容:斗图啦全网图片 使用工具:requests ...
随机推荐
- HDU:过山车(二分图最大匹配)
http://acm.hdu.edu.cn/showproblem.php?pid=2063 题意:有m个男,n个女,和 k 条边,求有多少对男女可以搭配. 思路:裸的二分图最大匹配,匈牙利算法. 枚 ...
- c#记事本
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- C#获取程序路径
// 获取程序的基目录. System.AppDomain.CurrentDomain.BaseDirectory // 获取模块的完整路径. System.Diagnostics.Process.G ...
- hiho 第118周 网络流四·最小路径覆盖
描述 国庆期间正是旅游和游玩的高峰期. 小Hi和小Ho的学习小组为了研究课题,决定趁此机会派出若干个调查团去沿途查看一下H市内各个景点的游客情况. H市一共有N个旅游景点(编号1..N),由M条单向游 ...
- ural 1113,jeep problem
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1113 网上的解答铺天盖地.我硬是花了两天才懂了点. wiki上的解释:https://e ...
- boost库学习之开篇
本系列文章使用boost_1.58.0版本. 一.欢迎使用boost C++库 boost致力于提供一个免费的.便携的源代码级的库. 我们重视那些与C++标准一起工作良好的库.boost库将要成为一个 ...
- NSIS学习记录の----NSIS多语言安装以及详解
NSIS多语言安装,很多教程提供了详细的代码,但是代码中某些语句的含义我还是不很明白,作为一个吃螃蟹的人,我做一个解释,避免很多小伙伴和我哟U一样的误区,以下结论都是自己根据实践得来,若发现理解错误, ...
- WebForm跨页面传值---内置对象
一.Response Response - 响应请求对象 string path = "Default2.aspx": (1)Response.Redirect(path); -- ...
- Linux 常见的trouble shooting故障排错
Linux 常见的trouble shooting故障排错 备份开机所必须运行的程序对一个运维人员来说是非常有必要的.在实际生产环境中,系统和数据基本都是安装在不同的硬盘上面,因为企业最关心的还是数据 ...
- Java中Properties类的操作配置文件
知识学而不用,就等于没用,到真正用到的时 候还得重新再学.最近在看几款开源模拟器的源码,里面涉及到了很多关于Properties类的引用,由于Java已经好久没用了,而这些模拟器大多用 Java来写, ...