Python 多线程下载脚本
2016-07-25 12:13
459 查看
#!/usr/bin/env python # coding:utf8 import requests import threading import sys class downloader: def __init__(self): ## 判断是否有位置参数或位置参数过多 if len(sys.argv) == 2: ## 第一参数为脚本 self.url = sys.argv[1] else: print 'Input error options...' sys.exit(1) ## 位置参数错误,输出并退出 self.num = 8 ## 如果需要自己手动输入线程数,可使用raw_input self.name = self.url.split('/')[-1] ##获取要下载的文件名 res = requests.head(self.url) self.total = int(res.headers['Content-Length']) ## 获取文件总长字节 ## 如果total为0,表示文件不存在,或路径错误,并退出 if self.total == 0: print "The error path..." sys.exit(2) print 'total is %s ' % (self.total) def get_range(self): list = [] offset = int(self.total/self.num) for i in range(self.num): if i == self.num-1: list.append((i*offset,'')) else: list.append((i*offset,(i+1)*offset)) return list def download(self,start,end): headers = {'Range':'Bytes=%s-%s' % (start,end),'Accept-Encoding':'*'} res = requests.get(self.url,headers=headers) print '%s:%s download success' % (start,end) self.fd.seek(start) ## 指针位置 self.fd.write(res.content) def rget(self): self.fd = open(self.name,'w') thread_lit = [] n = 0 for ran in self.get_range(): start,end = ran print 'thread %d start:%s,end:%s' % (n,start,end) n+=1 thread = threading.Thread(target=self.download,args=(start,end)) thread.start() thread_lit.append(thread) for j in thread_lit: j.join() print 'download %s load success' %(self.name) self.fd.close() if __name__ == '__main__': down =downloader() down.rget()自己改版,可能有所不足,其源脚本位置:https://github.com/shengxinjing/my_blog/blob/master/downloader/downloader.py。
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例