Terminado

0.18.1 · active · verified Sat Mar 28

Terminado is a Tornado websocket backend for the Xterm.js Javascript terminal emulator library. It enables web applications to embed interactive terminal sessions by exposing a shell (like bash or cmd.exe) over WebSockets. The library is currently at version 0.18.1 and receives regular maintenance and upkeep releases, typically focusing on dependency updates and minor bug fixes.

Warnings

Install

Imports

Quickstart

This quickstart sets up a basic Tornado web application with a `TermSocket` handler. It creates a single terminal instance managed by `SingleTermManager` and exposes it over a WebSocket endpoint. A simple HTTP handler is also included for initial access. Note that a client-side JavaScript terminal emulator (like Xterm.js) is required to interact with this backend. The `TERMINADO_SHELL`, `PORT`, and `HOST` environment variables can be used to customize the shell, port, and host respectively.

import os.path
import tornado.web
import tornado.ioloop
from terminado.websocket import TermSocket
from terminado.management import SingleTermManager

# NOTE: For a full web demo with a client-side terminal, you would also need
# tornado_xstatic and XStatic-term.js, and a corresponding HTML template.
# This example focuses on the Python server-side setup.

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello from Terminado server!\n")
        self.write("To use, connect a WebSocket-based terminal client (e.g., Xterm.js) to ws://localhost:8765/websocket")

if __name__ == "__main__":
    # Use 'bash' for Linux/macOS or 'cmd.exe' for Windows
    shell_command = os.environ.get('TERMINADO_SHELL', 'bash' if os.name != 'nt' else 'cmd.exe')
    term_manager = SingleTermManager(shell_command=[shell_command])

    handlers = [
        (r"/websocket", TermSocket, {"term_manager": term_manager}),
        (r"/", MainHandler),
    ]

    app = tornado.web.Application(handlers)
    port = int(os.environ.get('PORT', 8765))
    host = os.environ.get('HOST', 'localhost') # Listen on localhost for security
    app.listen(port, host)
    print(f"Terminado server started on http://{host}:{port}")
    print(f"WebSocket endpoint at ws://{host}:{port}/websocket")
    print(f"Using shell: {shell_command}")
    print("Press Ctrl+C to stop.")
    try:
        tornado.ioloop.IOLoop.current().start()
    except KeyboardInterrupt:
        print("\nServer stopped.")
    finally:
        term_manager.shutdown()

view raw JSON →