Terminal / app /main.py
goingyt's picture
Update app/main.py
215c411 verified
import os
import subprocess
from flask import Flask, render_template
from flask_socketio import SocketIO, emit
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
@app.route('/')
def index():
return render_template('index.html')
@socketio.on('input', namespace='/terminal')
def handle_input(data):
command = data['command']
print(f"Received command: {command}") # Debugging log
result = execute_command(command)
emit('output', {'output': result})
def execute_command(command):
try:
result = subprocess.run(['docker', 'exec', 'terminal-website_ubuntu_1', 'bash', '-c', command],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
output = result.stdout.decode('utf-8') + result.stderr.decode('utf-8')
print(f"Command output: {output}") # Debugging log
return output
except subprocess.CalledProcessError as e:
error_output = e.stdout.decode('utf-8') + e.stderr.decode('utf-8')
print(f"Command error: {error_output}") # Debugging log
return error_output
if __name__ == '__main__':
socketio.run(app, host='0.0.0.0', port=7860)