Spaces:
Sleeping
Sleeping
Oscar Wang
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -4,9 +4,8 @@ import shutil
|
|
4 |
from zipfile import ZipFile
|
5 |
import logging
|
6 |
import psutil
|
7 |
-
from flask import Flask, request, jsonify, render_template, send_file
|
8 |
-
import multiprocessing
|
9 |
import subprocess
|
|
|
10 |
|
11 |
# Configure logging
|
12 |
logging.basicConfig(level=logging.INFO)
|
@@ -91,6 +90,34 @@ def get_cpu_info():
|
|
91 |
logger.error(f"Error in get_cpu_info: {e}")
|
92 |
return jsonify({"status": "error", "message": str(e)}), 500
|
93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
# Main interface
|
95 |
@app.route('/')
|
96 |
def index():
|
|
|
4 |
from zipfile import ZipFile
|
5 |
import logging
|
6 |
import psutil
|
|
|
|
|
7 |
import subprocess
|
8 |
+
from flask import Flask, request, jsonify, render_template, send_file
|
9 |
|
10 |
# Configure logging
|
11 |
logging.basicConfig(level=logging.INFO)
|
|
|
90 |
logger.error(f"Error in get_cpu_info: {e}")
|
91 |
return jsonify({"status": "error", "message": str(e)}), 500
|
92 |
|
93 |
+
# Endpoint to execute commands
|
94 |
+
@app.route('/execute_command', methods=['POST'])
|
95 |
+
def execute_command():
|
96 |
+
try:
|
97 |
+
command = request.form['command']
|
98 |
+
if not command:
|
99 |
+
return jsonify({"status": "error", "message": "No command provided"}), 400
|
100 |
+
|
101 |
+
# Ensure commands are executed in a safe environment
|
102 |
+
allowed_commands = ['pip install']
|
103 |
+
if not any(command.startswith(cmd) for cmd in allowed_commands):
|
104 |
+
return jsonify({"status": "error", "message": "Command not allowed"}), 400
|
105 |
+
|
106 |
+
output_log = tempfile.TemporaryFile(mode='w+t')
|
107 |
+
try:
|
108 |
+
result = subprocess.run(command.split(), stdout=output_log, stderr=subprocess.STDOUT)
|
109 |
+
output_log.seek(0)
|
110 |
+
log_output = output_log.read()
|
111 |
+
except Exception as e:
|
112 |
+
log_output = str(e)
|
113 |
+
finally:
|
114 |
+
output_log.close()
|
115 |
+
return jsonify({"status": "success", "log_output": log_output})
|
116 |
+
|
117 |
+
except Exception as e:
|
118 |
+
logger.error(f"Error in execute_command: {e}")
|
119 |
+
return jsonify({"status": "error", "message": str(e)}), 500
|
120 |
+
|
121 |
# Main interface
|
122 |
@app.route('/')
|
123 |
def index():
|