import requests import tqdm as tqdm import os url = 'http://mirrors.163.com/centos/8.3.2011/isos/x86_64/CentOS-8.3.2011-x86_64-boot.iso' def download(url): filename = url.split('/')[-1] total_size = int(requests.head(url).headers['Content-Length']) if os.path.exists(filename): file_size = os.path.getsize(filename) if file_size < total_size: print('断点续传中。。。') elif file_size == total_size: print('文件已存在') exit(0) else: file_size = 0 header = {'Range': 'bytes=%s-%s' % (file_size, total_size)} t = tqdm.tqdm(total=total_size, desc=filename, initial=file_size, unit='B', unit_scale=True) result = requests.get(url, headers=header, stream=True) with open(filename, 'ab') as f: for i in result.iter_content(chunk_size=1024): f.write(i) t.update(1024) t.close() if __name__ == '__main__': url = 'http://mirrors.163.com/centos/8.3.2011/isos/x86_64/CentOS-8.3.2011-x86_64-boot.iso' download(url)