flask下载小服务
Posted 小蜗牛爱远行
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了flask下载小服务相关的知识,希望对你有一定的参考价值。
flask下载小服务,简单后端缓存判断
-
文件构成:
- 程序文件夹
- templates文件夹
***.html
__ini__.py
downRecordings.py
(flask服务)***.py
相关调用程序文件
- templates文件夹
- 程序文件夹
-
主程序代码
import flask import os import sys import json import datetime from flask import request from flask import Response from flask import render_template from flask import redirect from flask import url_for from get_recordings import getRecordings from flask_caching import Cache interface_path = os.path.dirname(__file__) sys.path.insert(0, interface_path) # 将当前文件的父目录加入临时系统变量 cache = Cache() app = flask.Flask(__name__) cache.init_app(app, config={'CACHE_TYPE': 'simple'}) def file_iterator(file_path, chunk_size=1024): """ 文件读取迭代器 :param file_path:文件路径 :param chunk_size: 每次读取流大小 :return: """ with open(file_path, 'rb') as target_file: while True: chunk = target_file.read(chunk_size) if chunk: yield chunk else: break def to_json(obj): """ 放置 :return: """ return json.dumps(obj, ensure_ascii=False) @app.route('/') def index(): return render_template('download.html') # 下载 @app.route('/download', methods=['GET']) def download(): ''' :param numbers: 手机号码,“,”隔开 :param nowtime: “2020-10-01”或“2020-01” :return: ''' if request.method == 'GET': numbers = request.values.get('phoneNumbers') nowtime = request.values.get('datetime') print(numbers, nowtime) flag = cache.get('flag') if flag == 1: #return redirect(url_for('waitting')) return render_template('waitting.html') else: cache.set('flag', 1, timeout=30) getRecordings(numbers, nowtime) file_name = datetime.datetime.now().strftime('%Y%m%d') + '.zip' file_path = os.path.join(os.getcwd(), file_name) if file_path is None: return to_json({'success': 0, 'message': '请输入参数'}) else: if file_path == '': return to_json({'success': 0, 'message': '请输入正确路径'}) else: if not os.path.isfile(file_path): return to_json({'success': 0, 'message': '文件路径不存在'}) else: filename = os.path.basename(file_path) response = Response(file_iterator(file_path)) response.headers['Content-Type'] = 'application/octet-stream' response.headers["Content-Disposition"] = 'attachment;filename="{}"'.format(filename) return response if __name__ == '__main__': print('录音下载程序正在运行') app.run(host='0.0.0.0', port=8000)
-
简单查询页面代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="http://code.jquery.com/jquery-1.10.2.js"></script> </head> <body> <h1>录音下载</h1> <form action="download" method="get"> <input type="text" placeholder="请输入手机号用逗号隔开" name="phoneNumbers"/> <input type="text" placeholder="请输入录音时间" name="datetime"/> <input type="submit" value="提交"> </form> </body> <script type="text/javascript"> // var $eleBtn1 = $("#btn1"); // var $eleBtn2 = $("#btn2"); //已知一个下载文件的后端接口: // https://codeload.github.com/douban/douban-client/legacy.zip/master //方法一:window.open() // $eleBtn2.click(function(){ // var phoneNumbers = document.getElementById('phoneNumbers').value; // var datetime = document.getElementById('datetime').value; // var url = 'http://localhost:8000/download?' + 'phoneNumbers=' + phoneNumbers + '&' + 'datetime=' + datetime; // // console.log(url); // // window.open(url); // }); </script> </html>
-
为了防止重复点击下载,简单做了后台的catch,做个请求的等待(有点鸡肋。。。)
以上是关于flask下载小服务的主要内容,如果未能解决你的问题,请参考以下文章