File size: 1,191 Bytes
7e0dee4
 
6badde7
7e0dee4
6badde7
 
7e0dee4
 
6badde7
 
 
 
 
7e0dee4
 
 
215c411
7e0dee4
 
 
 
215c411
 
 
 
 
 
 
 
 
 
7e0dee4
6badde7
7e0dee4
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
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)