好好百科

分享有趣又有用的知识

用户工具

站点工具


侧边栏

导航菜单

python:pypi:flask

flask

生成SECRET_KEY

import os
print(os.urandom(24).hex())                     

动态生成zip

import flask
 
app = flask.Flask('app')
app.config['TEMPLATES_AUTO_RELOAD'] = True
app.config['DEBUG'] = True
 
 
@app.route('/')
def index():
 
    def data():
        import io
        import base64
        import zipfile
        zip_file = io.BytesIO()
        with zipfile.ZipFile(zip_file, 'w') as _zip_file:
            _zip_file.writestr('hello.txt', 'world')
        zip_file.seek(0)
        return 'data:application/zip;base64,' + base64.b64encode(zip_file.read()).decode()
 
    return flask.make_response(
        flask.render_template_string('''
        <style>
        body {
            margin:0;
            padding:0;
            height:100vh;
            display:flex;
            align-items:center;
            justify-content:center;
        }
        </style>
        <button id="download">download</button>
        <script src="http://192.168.0.3:51888/static/jquery/3.3.1/jquery.min.js"></script>
        <script>
        $(function(){
            $("#download").click(function(){
                $("body").append($('<a id="file" href="'''+data()+'''" style="display:none" download="hello.zip"></a>'));
                document.getElementById("file").click();
                $("#file").remove();
            });
        })
        </script>'''), 200)
python/pypi/flask.txt · 最后更改: 2019/08/23 15:19 由 老赵