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()
相关推荐
眼眸流转18 分钟前
MCP学习笔记
python·uv·pydantic·mcp
千禧皓月24 分钟前
huggingface-cli下载数据集和模型
python
码界奇点29 分钟前
基于Spring Boot与Vue的校园后台管理系统设计与实现
vue.js·spring boot·后端·毕业设计·源代码管理
爱编程的小庄31 分钟前
Rust 发行版本及工具介绍
开发语言·后端·rust
DREAM依旧37 分钟前
本地微调的Ollama模型部署到Dify平台上
人工智能·python
小陈phd38 分钟前
langGraph从入门到精通(九)——基于LangGraph构建具备多工具调用与自动化摘要能力的智能 Agent
人工智能·python·langchain
一晌小贪欢38 分钟前
Python 对象的“Excel 之旅”:使用 openpyxl 高效读写与封装实战
开发语言·python·excel·表格·openpyxl·python办公·读取表格
【赫兹威客】浩哥40 分钟前
【赫兹威客】Python解释器部署教程
python
代码or搬砖44 分钟前
Prompt(提示词工程)
人工智能·python·prompt
喵手1 小时前
Python爬虫零基础入门【第二章:网页基础·第3节】接口数据基础:JSON 是什么?分页是什么?
爬虫·python·python爬虫实战·python爬虫工程化实战·python爬虫零基础入门·接口数据基础·爬虫json