Repository navigation
API 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_addressrefuses 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
Hostheader must name the address served (another name pointed at 127.0.0.1, DNS rebinding, is refused) and the WebSocket'sOriginmust 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.
COOKIE, COOKIE_TTL, MAX_MESSAGE, QUIT_GRACE, STATIC, Session, TOKEN_TTL, WebServer, page_html
'turboframe_session'
43200.0
1048576
5.0
<the turboframe package>/web/static (a Path)
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() -> Noneasync 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 afterQUIT_GRACE. -
failure() -> str: The end of the application's error output, when it ended with an error.
600.0
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):COOKIEand 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]: TheHostheaders that name this server: its address, andlocalhostfor a loopback one. make_app() -> web.Applicationasync index(request: web.Request) -> web.StreamResponseasync static(request: web.Request) -> web.StreamResponseasync websocket(request: web.Request) -> web.StreamResponse-
async start() -> None: Bind and serve (OSErrorwhen the address cannot be bound); forserveand 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;OSErrorwhen the address cannot be bound (a port in use).
def page_html(title: str, res: tuple[int, int] | None) -> strThe page: a terminal, fixed at res cells or following the window.
TurboFrame · part of Divengine · GPL-3.0-or-later · report an issue
Tutorial
Examples
The desktop
- The Desktop
- Application Mixins
- Windows and Lists
- Dialogs
- Histories
- Progress
- Themes and Colors
- Glyphs
- Settings and Recent Files
- Command Palette
Editing
Tasks
Languages
Running
Reference
Links