python写http文件下载器_http分片请求-python分片下载文件

源文件

http://theday.guohongfu.top/letter.txt内容为abcdefghijklmnopqrstuvwxyz

获取第20字节及以后的内容import requests

url = 'http://theday.guohongfu.top/letter.txt'

headers1 = {

'Range': "bytes=20-" # 获取 第20字节及以后的

}

response = requests.get(url, headers=headers1)

print('data={}'.format(response.content.decode())) # abcdef

# 结果

#data=uvwxyz

设置 If-Match 判断文件在两次请求间是否发生了改变import requests

url = 'http://theday.guohongfu.top/letter.txt'

headers1 = {

'Range': "bytes=0-5" # 获取0-5 的字节

}

response = requests.get(url, headers=headers1)

print('data={}'.format(response.content.decode())) # abcdef

# 得到etag

req_etag = response.headers['ETag']

headers1['If-Match'] = req_etag # 判断文件在两次请求间是否发生了改变

headers1['Range'] = 'bytes=6-10' # 获取6-10字节的数据

response = requests.get(url, headers=headers1)

print('data={}'.format(response.content.decode())) # ghijk

得到结果:# data=abcdef

# data=ghijk

使用 Python 分片下载文件import requests

mp4url = 'https://mp4.vjshi.com/2020-11-20/1c28d06e0278413bf6259ba8b9d26140.mp4'

response = requests.get(mp4url, stream=True)

with open('test.mp4', 'wb') as f:

[f.write(chunk) for chunk in response.iter_content(chunk_size=512) if chunk]每次以512字节进行下载数据,防止下载文件过大而被一次性读取到内存中,导致内存爆满。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值