-
shell
wget -c http://xxx.xxx.xxx
-
python断点续传
# -*- coding: utf-8 -*-
# 断点续传
import sys
import requests
import os
import time
# 屏蔽warning信息
requests.packages.urllib3.disable_warnings()
def download(url, file_path):
res = {
"function":"download",
"status":True
}
res["url"] = url
res["save_path"] = file_path
start = time.clock()
# 第一次请求是为了得到文件总大小
total_size = 0
try:
r1 = requests.get(url, stream=True, verify=False)
total_size = int(r1.headers['Content-Length'])
except BaseException as e:
print(e)
res["status"] = False
print("服务器端无法获取文件大小!!")
print(res)
return res
# 这重要了,先看看本地文件下载了多少
if os.path.exists(file_path):
temp_size = os.path.getsize(file_path) # 本地已经下载的文件大小
else:
temp_size = 0
# 显示一下下载了多少
print("total_size:", total_size)
print("downed_size:", temp_size)
res["file_size"] = total_size
# 核心部分,这个是请求下载时,从本地文件已经下载过的后面下载
headers = {'Range': 'bytes=%d-' % temp_size}
# 重新请求网址,加入新的请求头的
try:
r = requests.get(url, stream=True, verify=False, headers=headers)
status_code = str(r.status_code)
print("http status_code:",status_code)
res["status_code"] = r.status_code
if status_code.startswith("404"):
res["status"] = False
print("服务器端无法获取文件!!")
print(res)
return res
except BaseException as e:
print(e)
res["status"] = False
res["status_code"] = 1000
print(res)
return res
# 下面写入文件也要注意,看到"ab"了吗?
# "ab"表示追加形式写入文件
with open(file_path, "ab") as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
temp_size += len(chunk)
f.write(chunk)
f.flush()
###这是下载实现进度显示####
done = int(50 * temp_size / total_size)
sys.stdout.write("\r[%s%s] %d%%" % ('█' * done, ' ' * (50 - done), 100 * temp_size / total_size))
sys.stdout.flush()
end = time.clock()
print() # 避免上面\r 回车符
res["download_duration"] = end - start
return res
if __name__ == '__main__':
# 远程文件路径
url = "http://www.baidu.com"
# 存储地址+文件名
path = './'
file_name = '113.mp4'
path = path + file_name
# 调用断点续传方法
download(url, path)
参考:
https://blog.csdn.net/richardysteven/article/details/4565931