Skip to content

API turboframe.web.server

rafageist edited this page Dec 4, 2026 · 2 revisions

turboframe.web.server

Source: turboframe/web/server.py. Generated from the code of TurboFrame 0.3.0 by tools/wiki_api.py.

The web server of --web: the application's sessions behind a page and a WebSocket (the web extra).

The protocol is Textual's own web driver (textual.drivers.web_driver): a session's process is the application started with TEXTUAL_DRIVER set; it writes packets on its standard output (D data for the terminal, M meta as JSON) after a __GANGLION__ line, and reads D (keys, as the terminal sends them) and M (resize, focus, blur, quit) packets on its standard input. The page is a terminal (xterm.js, served from this package) that shows the data and sends the keys.

What protects it, since a browser page can talk to any port of this machine:

  • it serves loopback addresses only (turboframe.web.parse_address refuses the others);
  • a secret token, printed once on the terminal in the URL to open, is exchanged on the first visit for an HttpOnly, SameSite=Strict cookie, and the page drops the token from its address; the page and the WebSocket need that cookie;
  • the Host header must name the address served (another name pointed at 127.0.0.1, DNS rebinding, is refused) and the WebSocket's Origin must be the page's own (no other site's page connects);
  • the page forbids every outside source (Content-Security-Policy): it loads its scripts and styles from here;
  • sessions are limited (max_sessions), messages too, and a session's process is the application's command, fixed when the server started: nothing the browser sends becomes a command, and no shell is involved;
  • no access log is kept, so the token is never written anywhere but the terminal that started the server.

A session ends with its tab: the application is asked to quit and, if it has not after QUIT_GRACE seconds, killed with what it started (its process group, on POSIX). Ctrl+C ends every session, then the server.

Contents

COOKIE, COOKIE_TTL, MAX_MESSAGE, QUIT_GRACE, STATIC, Session, TOKEN_TTL, WebServer, page_html

COOKIE

'turboframe_session'

COOKIE_TTL

43200.0

MAX_MESSAGE

1048576

QUIT_GRACE

5.0

STATIC

<the turboframe package>/web/static (a Path)

class Session

class Session
Session(server: 'WebServer', socket: web.WebSocketResponse, size: tuple[int, int])

One browser tab: the application's process and the WebSocket it talks through.

  • async start() -> None
  • async meta(data: dict) -> None
  • async pump_output() -> None: The application's packets to the browser, until it ends (then the page says the session ended).
  • async pump_errors() -> None
  • async handle(message: str) -> None: A message from the page: ["stdin", text], ["resize", {"width", "height"}], ["focus"], ["blur"] or ["ping", value]. Anything else is ignored.
  • async stop() -> None: Ask the application to quit; kill it, and what it started, if it has not after QUIT_GRACE.
  • failure() -> str: The end of the application's error output, when it ended with an error.

TOKEN_TTL

600.0

class WebServer

class WebServer
WebServer(argv: list[str], options: WebOptions, *, title: str | None = None, max_sessions: int = 4, cwd: str | None = None)

Serve argv (an application's command, started once per browser tab) on options' address.

  • cookie_name (property): COOKIE and the port: a browser sends a host's cookies to every port of it, so two servers on one machine (two applications, or one at two sizes) would otherwise overwrite each other's cookie.
  • url (property): The address to open, with the token.
  • allowed_hosts() -> set[str]: The Host headers that name this server: its address, and localhost for a loopback one.
  • make_app() -> web.Application
  • async index(request: web.Request) -> web.StreamResponse
  • async static(request: web.Request) -> web.StreamResponse
  • async websocket(request: web.Request) -> web.StreamResponse
  • async start() -> None: Bind and serve (OSError when the address cannot be bound); for serve and the tests.
  • async stop() -> None: End every session, then the server.
  • async run(*, open_browser: bool = False) -> None
  • serve(*, open_browser: bool = False) -> None: Serve until Ctrl+C; OSError when the address cannot be bound (a port in use).

page_html

def page_html(title: str, res: tuple[int, int] | None) -> str

The page: a terminal, fixed at res cells or following the window.

Clone this wiki locally