Python简易HTTP文件服务器
我超怕的 - 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
查看,浏览器访问:http://ip:9999/list
下载,浏览器访问:http://ip:9999/uploads/文件名
【请勿用于生产环境】
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)
俗人昭昭,我独昏昏。俗人察察,我独闷闷。