u
File size: 1,155 Bytes
294aaf0
 
 
8d225c3
294aaf0
 
 
8d225c3
294aaf0
 
 
8d225c3
294aaf0
 
8d225c3
294aaf0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b288a2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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)