Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,12 @@
|
|
1 |
from flask import Flask, request, jsonify, render_template
|
2 |
-
import
|
3 |
-
import sys
|
4 |
import subprocess
|
5 |
import tempfile
|
6 |
import shutil
|
7 |
-
import os
|
8 |
|
9 |
app = Flask(__name__)
|
10 |
|
11 |
-
# Create a temporary directory for
|
12 |
temp_dir = tempfile.mkdtemp()
|
13 |
|
14 |
@app.route("/")
|
@@ -17,46 +15,46 @@ def index():
|
|
17 |
|
18 |
@app.route("/execute", methods=["POST"])
|
19 |
def execute_code():
|
20 |
-
|
21 |
-
|
22 |
-
sys.stdout = output
|
23 |
-
sys.stderr = output
|
24 |
|
25 |
try:
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
39 |
else:
|
40 |
-
#
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
44 |
except Exception as e:
|
45 |
-
|
46 |
-
finally:
|
47 |
-
# Reset stdout and stderr
|
48 |
-
sys.stdout = sys.__stdout__
|
49 |
-
sys.stderr = sys.__stderr__
|
50 |
|
51 |
-
return jsonify({"result":
|
52 |
|
53 |
@app.route("/cleanup", methods=["POST"])
|
54 |
def cleanup():
|
55 |
-
#
|
56 |
global temp_dir
|
57 |
if os.path.exists(temp_dir):
|
58 |
shutil.rmtree(temp_dir)
|
59 |
-
temp_dir = tempfile.mkdtemp()
|
60 |
return jsonify({"result": "Temporary files cleaned up."})
|
61 |
|
62 |
if __name__ == "__main__":
|
|
|
1 |
from flask import Flask, request, jsonify, render_template
|
2 |
+
import os
|
|
|
3 |
import subprocess
|
4 |
import tempfile
|
5 |
import shutil
|
|
|
6 |
|
7 |
app = Flask(__name__)
|
8 |
|
9 |
+
# Create a temporary directory for operations
|
10 |
temp_dir = tempfile.mkdtemp()
|
11 |
|
12 |
@app.route("/")
|
|
|
15 |
|
16 |
@app.route("/execute", methods=["POST"])
|
17 |
def execute_code():
|
18 |
+
command = request.json.get("code", "").strip()
|
19 |
+
response = ""
|
|
|
|
|
20 |
|
21 |
try:
|
22 |
+
# Ensure all operations happen within the temporary directory
|
23 |
+
os.chdir(temp_dir)
|
24 |
+
|
25 |
+
if command.startswith("!"):
|
26 |
+
# Handle shell commands (e.g., git clone, cd, pip install)
|
27 |
+
shell_command = command[1:]
|
28 |
+
process = subprocess.run(
|
29 |
+
shell_command,
|
30 |
+
shell=True,
|
31 |
+
stdout=subprocess.PIPE,
|
32 |
+
stderr=subprocess.PIPE,
|
33 |
+
text=True,
|
34 |
+
)
|
35 |
+
response = process.stdout + process.stderr
|
36 |
else:
|
37 |
+
# Handle Python code execution
|
38 |
+
process = subprocess.run(
|
39 |
+
["python3", "-c", command],
|
40 |
+
stdout=subprocess.PIPE,
|
41 |
+
stderr=subprocess.PIPE,
|
42 |
+
text=True,
|
43 |
+
)
|
44 |
+
response = process.stdout + process.stderr
|
45 |
+
|
46 |
except Exception as e:
|
47 |
+
response = f"Error: {e}"
|
|
|
|
|
|
|
|
|
48 |
|
49 |
+
return jsonify({"result": response})
|
50 |
|
51 |
@app.route("/cleanup", methods=["POST"])
|
52 |
def cleanup():
|
53 |
+
# Clean up the temporary directory and recreate it
|
54 |
global temp_dir
|
55 |
if os.path.exists(temp_dir):
|
56 |
shutil.rmtree(temp_dir)
|
57 |
+
temp_dir = tempfile.mkdtemp()
|
58 |
return jsonify({"result": "Temporary files cleaned up."})
|
59 |
|
60 |
if __name__ == "__main__":
|