Artificial-superintelligence commited on
Commit
6dea265
·
verified ·
1 Parent(s): 5052a97

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -39
app.py CHANGED
@@ -1,13 +1,10 @@
1
- from flask import Flask, request, jsonify, render_template
2
  import os
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,56 +13,55 @@ 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)
 
1
+ from flask import Flask, request, jsonify, render_template, stream_with_context, Response
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()
 
13
  def index():
14
  return render_template("index.html")
15
 
16
+ def execute_shell_command(command):
17
+ """Executes a shell command and streams output."""
18
+ process = subprocess.Popen(
19
+ command,
20
+ shell=True,
21
+ stdout=subprocess.PIPE,
22
+ stderr=subprocess.PIPE,
23
+ text=True,
24
+ cwd=temp_dir
25
+ )
26
+ for line in iter(process.stdout.readline, ""):
27
+ yield f"{line}<br>"
28
+ for line in iter(process.stderr.readline, ""):
29
+ yield f"Error: {line}<br>"
30
+ process.stdout.close()
31
+ process.stderr.close()
32
+ process.wait()
33
 
34
+ @app.route("/execute", methods=["POST"])
35
+ def execute_code():
36
+ command = request.json.get("code", "").strip()
37
+ if not command:
38
+ return jsonify({"result": "Error: No command provided."})
39
 
40
+ try:
41
+ # If the command starts with "!", treat it as a shell command
42
  if command.startswith("!"):
43
  shell_command = command[1:]
44
+ return Response(stream_with_context(execute_shell_command(shell_command)))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  else:
46
+ # Treat the command as Python code
47
+ process = subprocess.run(
48
  ["python3", "-c", command],
49
  stdout=subprocess.PIPE,
50
  stderr=subprocess.PIPE,
51
  text=True,
52
+ cwd=temp_dir
53
  )
54
+ return jsonify({"result": process.stdout + process.stderr})
 
 
 
 
 
55
  except Exception as e:
56
+ return jsonify({"result": f"Error: {e}"})
57
 
58
+ @app.route("/cleanup", methods=["POST"])
59
  def cleanup():
60
  global temp_dir
61
  if os.path.exists(temp_dir):
62
  shutil.rmtree(temp_dir)
63
+ temp_dir = tempfile.mkdtemp() # Recreate for a new session
64
+ return jsonify({"result": "Temporary files cleaned up."})
65
 
66
  if __name__ == "__main__":
67
+ app.run(host="0.0.0.0", port=7860)