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()
相关推荐
@––––––1 分钟前
力扣hot100—系列6-栈
linux·python·leetcode
AustinZhai2 分钟前
从时间轮定时器到网络产品
后端
Jia ming3 分钟前
《智能法官软件项目》—数据可视化模块
python·信息可视化·教学·案例·智能法官软件
中国胖子风清扬14 分钟前
GPUI 在 macOS 上编译问题排查指南
spring boot·后端·macos·小程序·rust·uni-app·web app
kong790692815 分钟前
SpringBoot Rest风格 API
java·spring boot·后端
Web极客码21 分钟前
CentOS 7 删除文件却不释放空间?从 inode、文件描述符到 VFS 的底层原理解析
python·centos·numpy
火红色祥云39 分钟前
Python机器学习经典实例_笔记
笔记·python·机器学习
yq1982043011561 小时前
基于Python爬虫原理的Pinterest视频资源获取技术解析与工具实践
爬虫·python·django·音视频
喵手1 小时前
Python爬虫实战:自动化质量护航 - 构建爬虫数据的“熔断与巡检”规则引擎实战!
爬虫·python·自动化·爬虫实战·零基础python爬虫教学·自动化质量护航·数据熔断
一切尽在,你来1 小时前
AI大模型应用开发前置知识:Python 异步编程
python·ai编程