import os import pexpect from flask import Flask, render_template from flask_socketio import SocketIO, emit app = Flask(__name__) socketio = SocketIO(app) @app.route('/') def index(): return render_template('index.html') @socketio.on('command') def handle_command(command): output = run_command(command) emit('output', output) def run_command(command): child = pexpect.spawn(command) child.timeout = 5 # Set a timeout for the command (adjust as needed) try: child.expect(pexpect.EOF) return child.before.decode('utf-8') except pexpect.exceptions.TIMEOUT: return 'Command timed out.' # print("running") # socketio.run(app, port=7860,allow_unsafe_werkzeug=True)