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()
    
相关推荐
waterHBO20 分钟前
python 爬虫 selenium 笔记
爬虫·python·selenium
编程零零七1 小时前
Python数据分析工具(三):pymssql的用法
开发语言·前端·数据库·python·oracle·数据分析·pymssql
AIAdvocate3 小时前
Pandas_数据结构详解
数据结构·python·pandas
小言从不摸鱼3 小时前
【AI大模型】ChatGPT模型原理介绍(下)
人工智能·python·深度学习·机器学习·自然语言处理·chatgpt
罗政5 小时前
[附源码]超简洁个人博客网站搭建+SpringBoot+Vue前后端分离
vue.js·spring boot·后端
FreakStudio5 小时前
全网最适合入门的面向对象编程教程:50 Python函数方法与接口-接口和抽象基类
python·嵌入式·面向对象·电子diy
拾光师6 小时前
spring获取当前request
java·后端·spring
redcocal6 小时前
地平线秋招
python·嵌入式硬件·算法·fpga开发·求职招聘
artificiali7 小时前
Anaconda配置pytorch的基本操作
人工智能·pytorch·python
RaidenQ7 小时前
2024.9.13 Python与图像处理新国大EE5731课程大作业,索贝尔算子计算边缘,高斯核模糊边缘,Haar小波计算边缘
图像处理·python·算法·课程设计