Artificial-superintelligence commited on
Commit
f28cbc7
·
verified ·
1 Parent(s): 78fe67b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -19
app.py CHANGED
@@ -3,8 +3,11 @@ 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()
@@ -13,49 +16,56 @@ temp_dir = tempfile.mkdtemp()
13
  def index():
14
  return render_template("index.html")
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__":
61
- app.run(host="0.0.0.0", port=7860)
 
3
  import subprocess
4
  import tempfile
5
  import shutil
6
+ from flask_socketio import SocketIO, emit
7
 
8
  app = Flask(__name__)
9
+ app.config['SECRET_KEY'] = 'secret!'
10
+ socketio = SocketIO(app)
11
 
12
  # Create a temporary directory for operations
13
  temp_dir = tempfile.mkdtemp()
 
16
  def index():
17
  return render_template("index.html")
18
 
19
+ @socketio.on("execute")
20
+ def execute_command(data):
21
+ command = data.get("code", "").strip()
22
  response = ""
23
 
24
  try:
25
+ os.chdir(temp_dir) # Ensure all operations happen within the temporary directory
26
+ emit("output", {"data": f"$ {command}\n"}, broadcast=True)
27
 
28
  if command.startswith("!"):
 
29
  shell_command = command[1:]
30
+ process = subprocess.Popen(
31
  shell_command,
32
  shell=True,
33
  stdout=subprocess.PIPE,
34
  stderr=subprocess.PIPE,
35
  text=True,
36
  )
37
+ # Stream output live
38
+ for line in process.stdout:
39
+ emit("output", {"data": line}, broadcast=True)
40
+ for line in process.stderr:
41
+ emit("output", {"data": line}, broadcast=True)
42
+
43
+ process.wait()
44
+ emit("output", {"data": "Command execution complete.\n"}, broadcast=True)
45
  else:
46
+ # Handle Python script execution
47
+ process = subprocess.Popen(
48
  ["python3", "-c", command],
49
  stdout=subprocess.PIPE,
50
  stderr=subprocess.PIPE,
51
  text=True,
52
  )
53
+ for line in process.stdout:
54
+ emit("output", {"data": line}, broadcast=True)
55
+ for line in process.stderr:
56
+ emit("output", {"data": line}, broadcast=True)
57
+ process.wait()
58
+ emit("output", {"data": "Python execution complete.\n"}, broadcast=True)
59
  except Exception as e:
60
+ emit("output", {"data": f"Error: {e}\n"}, broadcast=True)
 
 
61
 
62
+ @socketio.on("cleanup")
63
  def cleanup():
 
64
  global temp_dir
65
  if os.path.exists(temp_dir):
66
  shutil.rmtree(temp_dir)
67
  temp_dir = tempfile.mkdtemp()
68
+ emit("output", {"data": "Temporary files cleaned up.\n"}, broadcast=True)
69
 
70
  if __name__ == "__main__":
71
+ socketio.run(app, host="0.0.0.0", port=7860)