python实现断点续传下载文件
2015-08-30 16:16
706 查看
最近的任务里有一个功能是要我从日志服务器实时跟新日志到本地,日志在不断新增内容就需要我隔一段时间从上次下载的位置继续下载,并写入本地文件上次写完的位置后面。
这里用的是requests.get()和他的一些参数
然后以
的方式逐chunk_size地遍历数据,并写入local_file
刷新也很重要,实时保证一点点的写入。
headers = {'Range': 'bytes=%d-' % local_file_dict.get(packet_path+k)} web_log = requests.get(packet_web_path+k, stream=True, headers=headers) with open(packet_path+k, 'ab') as local_file: for chunk in web_log.iter_content(chunk_size=1024): if chunk: local_file.write(chunk) local_file.flush()
这里用的是requests.get()和他的一些参数
requests.get(url, stream=True, headers=headers)要实现断点续传,get()的stream参数要设为True在远程打开的是一个流,而headers里放的是续传的一些参数,这里的
headers = {'Range': 'bytes=%d-' % local_file_size}就是获得本地文件的大小作为续传的起点,还有就是按bytes
然后以
iter_content(chunk_size=xxx)
的方式逐chunk_size地遍历数据,并写入local_file
local_file.flush()
刷新也很重要,实时保证一点点的写入。
相关文章推荐
- python 内置函数
- multiprocessing在python中的高级应用-进程池
- Python入门
- Python编程错误之IndentationError: unexpected indent
- python switch
- 【python】17个新手常见Python运行时错误
- multiprocessing在python中的高级应用-IPC 之 Pipe
- python Lambda 表达式
- 一步步学习Python-django开发-添加后台管理
- Python, difference between two ways for removing last elem of list
- python需要注意的语法
- python执行时出现的问题
- 【Python】笔记:类继承问题总结
- selenium +python+eclipse+pydev环境搭建
- multiprocessing在python中的高级应用-IPC 之 Queue
- 学习日志---python(新式类,面向对象)
- Python getopt模块
- 5.Python的Queue模块
- python 排序归纳
- 一个简单的程序,统计文本文档中的单词和汉字数,逆序排列(出现频率高的排在最前面)。python实现。