flask文件下载

一、定义

  1. flask 文件下载
  2. python 接收tar 文件

二、实现

  1. flask 文件下载

    from flask import Flask, send_file, request
    import os
    from gevent.pywsgi import WSGIServer
    import logging

    logging.basicConfig(level=logging.INFO, format="%(asctime)s %(filename)s [line:%(lineno)d] %(levelname)s %(message)s")
    app = Flask(name)

    @app.route('/download', methods=["GET"])
    def download_tar():
    # 设置tar文件的路径
    tar_file_path = request.args.get("filename")
    tar_file_path = os.path.join("/home/jiayafei_linux/downloads_files", tar_file_path)
    logging.info(str(tar_file_path))
    # 检查文件是否存在
    if not os.path.isfile(tar_file_path):
    return "File not found", 404
    # 设置文件名,这将作为下载时的默认文件名
    filename = os.path.basename(tar_file_path)
    logging.info(str(filename))
    # 使用send_file发送文件
    return send_file(tar_file_path, as_attachment=True, download_name=filename)

    if name == 'main':
    print("start")
    http_server = WSGIServer(('0.0.0.0', int(8010)), app)
    http_server.serve_forever()

  2. python 接收tar 文件

    #接收tar 文件,并保存到本地 baike_10000k_0.tar
    def download_file_with_progress(self, url, save_path):
    response = requests.get(url, stream=True)
    total_size_in_bytes = int(response.headers.get('content-length', 0))
    block_size = 1024 # 1 Kibibyte
    progress_bar = tqdm(total=total_size_in_bytes, unit='iB', unit_scale=True)

    复制代码
         with open(save_path, 'wb') as file:
             for data in response.iter_content(block_size):
                 progress_bar.update(len(data))
                 file.write(data)
         progress_bar.close()
相关推荐
幻影123!11 分钟前
AlphaZero 五子棋实战(番外二):白棋专项训练的一种方案
python·机器学习·强化学习·五子棋·alpha zero
谢亮_vipxieliang12 分钟前
PHP 8 新特性:构造器属性提升与实战
开发语言·后端·php
imDwAaY14 分钟前
Spring Boot的核心配置文件一览及其加载顺序
java·spring boot·后端
头发够用的程序员16 分钟前
TensorRT 自定义算子插件实战(三):手搓 2×2 最大池化 customMaxpool
人工智能·pytorch·python·深度学习·神经网络·边缘计算·jetson
鬼手点金21 分钟前
opencode-crawl4ai使用建议
java·开发语言·人工智能·python·机器学习
高频因子挖掘机27 分钟前
量化选股到底要不要每天拉全市场股票?从全市场扫描到候选池的行情数据设计
后端·github·api
海天一色y29 分钟前
深入理解对偶单纯形法:原理推导与 Python/MATLAB 编程实现
python·matlab·最优化算法·对偶单纯形法
专业程序开发源36 分钟前
springboot校园志愿者管理系统28562-计算机课程设计、毕业设计
java·spring boot·后端·python·django·php·课程设计
谢亮_vipxieliang41 分钟前
Go select 多路复用:从语法到实战的完整指南
开发语言·后端·golang
高频因子挖掘机1 小时前
复权价格怎么算?从除权因子、时间方向到量化回测避坑
后端·github·api