python实现断点续传下载文件

最近的任务里有一个功能是要我从日志服务器实时跟新日志到本地,日志在不断新增内容就需要我隔一段时间从上次下载的位置继续下载,并写入本地文件上次写完的位置后面。

[python] view plain copy
 
  1. headers = {'Range': 'bytes=%d-' % local_file_dict.get(packet_path+k)}  
  2. web_log = requests.get(packet_web_path+k, stream=True, headers=headers)  
  3. with open(packet_path+k, 'ab') as local_file:  
  4.     for chunk in web_log.iter_content(chunk_size=1024):  
  5.         if chunk:  
  6.             local_file.write(chunk)  
  7.             local_file.flush()  


这里用的是requests.get()和他的一些参数

[python] view plain copy
 
  1. requests.get(url, stream=True, headers=headers)  

要实现断点续传,get()的stream参数要设为True在远程打开的是一个流,而headers里放的是续传的一些参数,这里的

[python] view plain copy
 
  1. headers = {'Range': 'bytes=%d-' % local_file_size}  

就是获得本地文件的大小作为续传的起点,还有就是按bytes

然后以

[python] view plain copy
 
  1. iter_content(chunk_size=xxx)  

的方式逐chunk_size地遍历数据,并写入local_file

 

[python] view plain copy
 
  1. local_file.flush()  

刷新也很重要,实时保证一点点的写入。

 

原文地址:https://www.cnblogs.com/skying555/p/6218384.html