2022-03-02 06:17:04 +00:00
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import datetime
|
|
|
|
from flask import Flask, flash, request, redirect, url_for
|
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
|
2022-03-02 06:17:04 +00:00
|
|
|
def run_command(command, wait=False):
|
|
|
|
print("{} - execute shell command:{}".format(datetime.datetime.now(), command))
|
|
|
|
lines = []
|
2022-03-22 16:39:58 +00:00
|
|
|
p = subprocess.Popen(
|
|
|
|
command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True
|
|
|
|
)
|
2022-03-02 06:17:04 +00:00
|
|
|
if wait:
|
2022-03-22 16:39:58 +00:00
|
|
|
for l in iter(p.stdout.readline, b""):
|
2022-03-02 06:17:04 +00:00
|
|
|
lines.append(l)
|
|
|
|
p.poll()
|
|
|
|
return (lines, p.returncode)
|
|
|
|
else:
|
2022-03-22 16:39:58 +00:00
|
|
|
return (iter(p.stdout.readline, b""), 0)
|
2022-03-02 06:17:04 +00:00
|
|
|
|
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
UPLOAD_FOLDER = "./"
|
|
|
|
ALLOWED_EXTENSIONS = {"txt", "sh"}
|
2022-03-02 06:17:04 +00:00
|
|
|
app = Flask(__name__)
|
2022-03-22 16:39:58 +00:00
|
|
|
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
|
|
|
|
|
2022-03-02 06:17:04 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
@app.route("/")
|
2022-03-02 06:17:04 +00:00
|
|
|
def hello_world():
|
2022-03-22 16:39:58 +00:00
|
|
|
return "Hello World"
|
2022-03-02 06:17:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
def allowed_file(filename):
|
2022-03-22 16:39:58 +00:00
|
|
|
return "." in filename and filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS
|
2022-03-02 06:17:04 +00:00
|
|
|
|
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
@app.route("/upload", methods=["GET", "POST"])
|
2022-03-02 06:17:04 +00:00
|
|
|
def upload_file():
|
2022-03-22 16:39:58 +00:00
|
|
|
if request.method == "POST":
|
2022-03-02 06:17:04 +00:00
|
|
|
# check if the post request has the file part
|
2022-03-22 16:39:58 +00:00
|
|
|
if "file" not in request.files:
|
|
|
|
flash("No file part")
|
2022-03-02 06:17:04 +00:00
|
|
|
return redirect(request.url)
|
2022-03-22 16:39:58 +00:00
|
|
|
file = request.files["file"]
|
2022-03-02 06:17:04 +00:00
|
|
|
# If the user does not select a file, the browser submits an
|
|
|
|
# empty file without a filename.
|
2022-03-22 16:39:58 +00:00
|
|
|
if file.filename == "":
|
|
|
|
flash("No selected file")
|
2022-03-02 06:17:04 +00:00
|
|
|
return redirect(request.url)
|
|
|
|
if file and allowed_file(file.filename):
|
|
|
|
filename = file.filename
|
2022-03-22 16:39:58 +00:00
|
|
|
file.save(os.path.join(app.config["UPLOAD_FOLDER"], filename))
|
|
|
|
return redirect(url_for("upload_file", name=filename))
|
|
|
|
return """
|
2022-03-02 06:17:04 +00:00
|
|
|
<!doctype html>
|
|
|
|
<title>Upload new File</title>
|
|
|
|
<h1>Upload new File</h1>
|
|
|
|
<form method=post enctype=multipart/form-data>
|
|
|
|
<input type=file name=file>
|
|
|
|
<input type=submit value=Upload>
|
|
|
|
</form>
|
2022-03-22 16:39:58 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/run", methods=["GET", "POST"])
|
2022-03-02 06:17:04 +00:00
|
|
|
def parse_request():
|
|
|
|
data = request.data # data is empty
|
|
|
|
run_command(data, wait=True)
|
2022-03-22 16:39:58 +00:00
|
|
|
return "Ok"
|
|
|
|
|
2022-03-02 06:17:04 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
app.run(port=5011)
|