我超怕的 - HTTP simple file server use Python - https://www.cnblogs.com/iAmSoScArEd/p/17745959.html
How to use
安装依赖:pip3 install flask
保存到文本simple_file_server.py后运行:python3 simple_file_server.py
浏览器访问:http://ip:9999
Code
from flask import Flask, request, send_file, render_template, jsonify
import os
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
files = request.files.getlist('file[]')
uploaded_files = []
for file in files:
if file:
filename = file.filename
file.save(os.path.join('uploads', filename))
uploaded_files.append(filename)
return ', '.join(uploaded_files) + ' uploaded successfully.'
return '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File Upload</title>
</head>
<body>
<h1>File Upload</h1>
<form action="/" method="post" enctype="multipart/form-data">
<input type="file" name="file[]" multiple>
<input type="submit" value="Upload">
</form>
</body>
</html>
'''
@app.route('/list', methods=['GET'])
def list_files():
files = os.listdir('uploads')
return jsonify({'files': files})
@app.route('/uploads/<filename>')
def get_file(filename):
return send_file(os.path.join('uploads', filename), as_attachment=True)
if __name__ == '__main__':
if not os.path.exists('uploads'):
os.makedirs('uploads')
app.run(debug=True,host="0.0.0.0",port=9999)
标签:files,__,HTTP,Python,filename,uploads,file,服务器,os
From: https://www.cnblogs.com/iAmSoScArEd/p/17745959.html