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()
相关推荐
rgb2gray9 分钟前
AI 的“诚实”指南:一文详解 Conformal Prediction (共形预测) 与 Split Conformal
人工智能·python·机器学习·数据分析·可解释·共性预测·一致性预测
hakesashou11 分钟前
python 如何使数组中的元素不重复
开发语言·python
Filotimo_12 分钟前
JWT的概念
java·开发语言·python
Agilex松灵机器人22 分钟前
持续更新|从零到玩转Moveit机械臂控制(一)
人工智能·python·机器人·学习方法
J_liaty29 分钟前
Redis公共方法详解
spring boot·redis·后端
喵手32 分钟前
《Python爬虫工程化实战》专栏导读|从“脚本能跑”到“系统能交付”:零基础也能做出可部署的 Python 爬虫!
爬虫·python·网络爬虫·爬虫实战·python爬虫·python爬虫工程化·爬虫实战教学
子午39 分钟前
【2026原创】卫星遥感图像识别系统+Python+深度学习+人工智能+算法模型+TensorFlow
人工智能·python·深度学习
小北方城市网39 分钟前
生产级 Spring Boot + MyBatis 核心配置模板
java·spring boot·redis·后端·spring·性能优化·mybatis
a努力。1 小时前
蚂蚁Java面试被问:流批一体架构的实现和状态管理
java·后端·websocket·spring·面试·职场和发展·架构