python/shell文件下载/断点续传

21 篇文章 0 订阅
  • 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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值