#!/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。
转载于:https://blog.51cto.com/erguang/1829587