python requests 断点续传下载
来源:互联网 发布:李昌钰有多厉害知乎 编辑:程序博客网 时间:2024/06/05 06:28
1.安装requests:
pip install requests
2.实例:
# # encoding:utf-8import requestsimport osimport timedef get_local_file_exists_size(local_path): try: lsize = os.stat(local_path).st_size except: lsize = 0 return lsizedef get_file_obj(down_link, offset): webPage = None try: headers = {'Range': 'bytes=%d-' % offset} webPage = requests.get(down_link, stream=True, headers=headers, timeout=120, verify=False) status_code = webPage.status_code if status_code in [200, 206]: webPage = webPage elif status_code == 416: print u"%s文件数据请求区间错误,status_code:%s" % (down_link, status_code) else: print u"%s链接有误,status_code:%s" % (down_link, status_code) except Exception as e: print u"无法链接:%s,e:%s" % (down_link, e) finally: return webPagedown_link = '' #下载链接file_size = 271768736 #文件总大小local_path = "/home/adger/image/test.mp4"while True: lsize = get_local_file_exists_size(local_path) if lsize == file_size: break webPage = get_file_obj(down_link, lsize) try: file_obj = open(local_path, 'ab+') except Exception as e: print u"打开文件:%s失败" % local_path break try: for chunk in webPage.iter_content(chunk_size=64 * 1024): if chunk: file_obj.write(chunk) else: break except Exception as e: time.sleep(5) file_obj.close() webPage.close()
1 0
- python requests 断点续传下载
- python--requests下载图片
- python requests 下载安装
- python 断点续传下载
- python实现断点续传下载文件
- python requests.get() 下载大文件
- Python 使用urllib2模块实现断点续传下载
- python 下载 图片、音乐、视频 和 断点续传
- python requests
- python requests
- Python Requests
- python Requests
- python Requests
- python requests
- Requests-Python
- 断点续传 下载
- 下载-断点续传
- 使用requests.get下载大文件-Python
- 测试鸡蛋的硬度:一道关于查找的面试题
- android Switch控件一直处于聚焦状态,将屏蔽Preference的点击事件(处理方式)_xp
- 3.农夫、狐狸、鸡、小米问题
- android 优秀控件以及开源项目
- 判断是否有重复字符 bool IsUniqueChars( String str) CC150 1.1
- python requests 断点续传下载
- timer+TimerTask+handler倒计时-----小例子深奥多
- perl学习笔记-----------------------(9)
- 高斯模糊算法的实现和优化
- 对查询得到的List数据,进行分页操作
- 面向对象三大基本特性,五大基本原则
- Mac系统安装Python3.5
- 4.梵塔问题:
- 创建虚拟网卡