Flask框架实现⽂件服务器(⽀持上传和下载)
最近测试的时候需要模拟⼀个⽂件服务器,⽀持⽂件的上传和下载功能。百度了⼀下各位⼤神的博客,结合公司的实际情况,写了⼀个⽂件服务器。
环境信息:python3.7.4 + Flask(1.1.1)
测试⼯具:requests(2.22.0)
⽬录结构:
image.png
代码如下:
import os
from string import Template
from flask import Flask, request, send_file
from werkzeug.utils import secure_filename
app = Flask(__name__)
pwd = os.path.dirname(__file__)
#定义⽂件的保存路径和⽂件名尾缀
UPLOAD_FOLDER = os.path.join(pwd,'save_file')
ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'}
HOST = "192.168.52.235"
PORT = 5000
@ute('/index')
def index():
"""
返回⼀个⽹页端提交的页⾯
:return:
"""
html = Template("""
<!DOCTYPE html>
<html>
<body>
<form action = "$HOST:$PORT/upload" method = "POST"
enctype = "multipart/form-data">
<input type = "file" name = "file" />
<input type = "submit"/>
</form>
</body>
</html>
getsavefilename""")
html = html.substitute({"HOST": HOST, "PORT": PORT})
return html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。