python3 flask 编写上传文件接口

依赖环境:python3

依赖包:flask,pyOpenSSL(pip install flask pyOpenSSL)

#coding=utf-8
import os
from flask import Flask, redirect, url_for, request, render_template, send_from_directory
import time
app = Flask(__name__)
BASE_PATH = '/tmp/tmp_file/'

@app.route("/upload",methods=["POST"])
def upload_file():
    try:
        f=request.files["file"]

        filename = os.path.join(BASE_PATH,"",f.filename)
        f.save(filename)
        print("{0} file upload succcessfully!".format(filename))
        time.sleep(1)
        return "file upload successfully!"
    except Exception as e:
        return e




if __name__ == '__main__':
    app.run(host="0.0.0.0",port="5555",debug=False,ssl_context="adhoc")

  

posted @ 2022-04-06 13:59  RoyFans  阅读(347)  评论(0编辑  收藏  举报