|
from flask import Flask, request, jsonify, render_template |
|
import os |
|
import subprocess |
|
import tempfile |
|
import shutil |
|
|
|
app = Flask(__name__) |
|
|
|
|
|
temp_dir = tempfile.mkdtemp() |
|
|
|
@app.route("/") |
|
def index(): |
|
return render_template("index.html") |
|
|
|
@app.route("/execute", methods=["POST"]) |
|
def execute_code(): |
|
command = request.json.get("code", "").strip() |
|
response = "" |
|
|
|
try: |
|
|
|
os.chdir(temp_dir) |
|
|
|
if command.startswith("!"): |
|
|
|
shell_command = command[1:] |
|
process = subprocess.run( |
|
shell_command, |
|
shell=True, |
|
stdout=subprocess.PIPE, |
|
stderr=subprocess.PIPE, |
|
text=True, |
|
) |
|
response = process.stdout + process.stderr |
|
else: |
|
|
|
process = subprocess.run( |
|
["python3", "-c", command], |
|
stdout=subprocess.PIPE, |
|
stderr=subprocess.PIPE, |
|
text=True, |
|
) |
|
response = process.stdout + process.stderr |
|
|
|
except Exception as e: |
|
response = f"Error: {e}" |
|
|
|
return jsonify({"result": response}) |
|
|
|
@app.route("/cleanup", methods=["POST"]) |
|
def cleanup(): |
|
|
|
global temp_dir |
|
if os.path.exists(temp_dir): |
|
shutil.rmtree(temp_dir) |
|
temp_dir = tempfile.mkdtemp() |
|
return jsonify({"result": "Temporary files cleaned up."}) |
|
|
|
if __name__ == "__main__": |
|
app.run(host="0.0.0.0", port=7860) |
|
|