Skip to content

PtyClient

Source: leap0/_sync/pty.py

PtyClient(transport: Transport)

Create and manage interactive terminal sessions inside a sandbox.

Connect via WebSocket for real-time bidirectional I/O, similar to SSH or a browser-based terminal.

Example:

sandbox = client.sandboxes.create()
session = sandbox.pty.create(cols=120, rows=30)
conn = sandbox.pty.connect(session.id)
conn.send("pwd
")
print(conn.recv().decode())
conn.close()

Attributes: None.

list(sandbox: SandboxRef) -> list[PtySession]

List all PTY sessions for a sandbox.

Args: sandbox: Sandbox ID or object.

Returns: object: Result returned by this operation.

create(sandbox: SandboxRef, *, session_id: str | None = None, cols: int | None = None, rows: int | None = None, cwd: str | None = None, envs: dict[str, str] | None = None, lazy_start: bool | None = None, http_timeout: float | None = None) -> PtySession

Create a terminal session with a shell process.

Args: sandbox: Sandbox ID or object. session_id: Session ID. Auto-generated if omitted. cols: Terminal columns. rows: Terminal rows. cwd: Starting directory. envs: Environment variables. lazy_start: Defer shell start until the first WebSocket connection. http_timeout: Optional HTTP request timeout in seconds for this SDK call.

Args: sandbox: Sandbox ID or object. session_id: PTY session identifier. http_timeout: Optional HTTP request timeout in seconds for this SDK call.

Returns: PtySession: Metadata for the created PTY session.

get(sandbox: SandboxRef, session_id: str) -> PtySession

Get details for a single PTY session.

Args: sandbox: Sandbox ID or object. session_id: PTY session identifier.

Returns: object: Result returned by this operation.

delete(sandbox: SandboxRef, session_id: str, http_timeout: float | None = None) -> None

Kill the shell process and remove the session.

Args: sandbox: Sandbox ID or object. session_id: PTY session identifier. http_timeout: Optional HTTP request timeout in seconds for this SDK call.

resize(sandbox: SandboxRef, session_id: str, *, cols: int, rows: int) -> PtySession

Change the terminal dimensions while connected.

Args: sandbox: Sandbox ID or object. session_id: PTY session identifier. cols: Parameter for this operation. rows: Parameter for this operation.

Returns: object: Result returned by this operation.

websocket_url(sandbox: SandboxRef, session_id: str) -> str

Build the WSS URL for connecting to a PTY session.

Args: sandbox: Sandbox ID or object. session_id: PTY session identifier.

Returns: object: Result returned by this operation.

connect(sandbox: SandboxRef, session_id: str, http_timeout: float | None = None, **kwargs: Any) -> PtyConnection

Open a WebSocket connection for interactive terminal I/O.

Returns a :class:PtyConnection with send, recv, and close methods. All messages are binary frames containing raw terminal bytes.

Args: sandbox: Sandbox ID or object. session_id: PTY session identifier. http_timeout: Optional WebSocket open timeout in seconds. **kwargs: Additional keyword arguments passed to websockets.sync.client.connect.

Returns: PtyConnection: Open WebSocket-backed PTY connection.