# -*- coding:utf-8 -*-# vim:et:ts=4:sw=4:#!/usr/bin/env python######################################################################__author__='ishenweiyan@qq.com'__create__='2019-09-19'__file__='download_demo.py'__license__='2019 All rights reserved.'__doc__='The test script.'#####################################################################importosfromurllib.requestimporturlopenimportrequestsfromtqdmimporttqdmfromcontextlibimportclosingclassProgressBar(object):def__init__(self,title,count=0.0,run_status=None,fin_status=None,total=100.0,unit='',sep='/',chunk_size=1.0):super(ProgressBar,self).__init__()self.info="[%s] %s%.2f%s%s%.2f%s"self.title=titleself.total=totalself.count=countself.chunk_size=chunk_sizeself.status=run_statusor""self.fin_status=fin_statusor" "*len(self.status)self.unit=unitself.seq=sepdef__get_info(self):# [名称] 状态 进度 单位 分割线 总数 单位_info=self.info%(self.title,self.status,self.count/self.chunk_size,self.unit,self.seq,self.total/self.chunk_size,self.unit)return_infodefrefresh(self,count=1,status=None):self.count+=count# if status is not None:self.status=statusorself.statusend_str="\r"ifself.count>=self.total:end_str='\n'self.status=statusorself.fin_statusprint(self.__get_info(),end=end_str)defdownload_from_url(url,dst):""" @param: url to download file @param: dst place to put the file """file_size=int(urlopen(url).info().get('Content-Length',-1))ifos.path.exists(dst):first_byte=os.path.getsize(dst)else:first_byte=0iffirst_byte>=file_size:returnfile_sizeheader={"Range":"bytes=%s-%s"%(first_byte,file_size)}pbar=tqdm(total=file_size,initial=first_byte,unit='B',unit_scale=True,desc=url.split('/')[-1])req=requests.get(url,headers=header,stream=True)with(open(dst,'ab'))asf:forchunkinreq.iter_content(chunk_size=1024):ifchunk:f.write(chunk)pbar.update(1024)pbar.close()returnfile_sizedef__main__():url='http://ftp.ncbi.nlm.nih.gov/gene/DATA/gene2ensembl.gz'file_name='gene2ensembl.gz'''' with closing(requests.get(url, stream=True)) as response: chunk_size = 1024 # 单次请求最大值 content_size = int(response.headers['content-length']) # 内容体总大小 progress = ProgressBar(file_name, total=content_size, unit="KB", chunk_size=chunk_size, run_status="正在下载", fin_status="下载完成") with open(file_name, "wb") as file: for data in response.iter_content(chunk_size=chunk_size): file.write(data) progress.refresh(count=len(data)) '''download_from_url(url,file_name)if__name__=="__main__":__main__()