import os import subprocess import datetime from flask import Flask, flash, request, redirect, url_for def run_command(command, wait=False): print("{} - execute shell command:{}".format(datetime.datetime.now(), command)) lines = [] p = subprocess.Popen( command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True ) if wait: for l in iter(p.stdout.readline, b""): lines.append(l) p.poll() return (lines, p.returncode) else: return (iter(p.stdout.readline, b""), 0) UPLOAD_FOLDER = "./" ALLOWED_EXTENSIONS = {"txt", "sh"} app = Flask(__name__) app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER @app.route("/") def hello_world(): return "Hello World" def allowed_file(filename): return "." in filename and filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS @app.route("/upload", methods=["GET", "POST"]) def upload_file(): if request.method == "POST": # check if the post request has the file part if "file" not in request.files: flash("No file part") return redirect(request.url) file = request.files["file"] # If the user does not select a file, the browser submits an # empty file without a filename. if file.filename == "": flash("No selected file") return redirect(request.url) if file and allowed_file(file.filename): filename = file.filename file.save(os.path.join(app.config["UPLOAD_FOLDER"], filename)) return redirect(url_for("upload_file", name=filename)) return """