u / app.py
BG5's picture
Update app.py
1b288a2 verified
raw
history blame
1.16 kB
from flask import Flask, render_template
from flask_socketio import SocketIO, emit
import subprocess
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
@app.route('/')
def index():
return render_template('index.html')
@socketio.on('execute_command')
def handle_command(command):
try:
# 执行命令并捕获输出
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
while True:
output = process.stdout.readline()
if output == '' and process.poll() is not None:
break
if output:
emit('command_output', {'output': output.strip()})
# 捕获错误输出
error = process.stderr.read()
if error:
emit('command_output', {'output': error.strip()})
# 发送命令执行结束信号
emit('command_output', {'output': '[Command execution finished]'})
except Exception as e:
emit('command_output', {'output': str(e)})
if __name__ == '__main__':
socketio.run(app, host='0.0.0.0', port=7860)