Sandbox. See Commands for usage examples.
Typical flow
Run and collect
sandbox.exec()
Example
Example
exec_stream() instead. Raises ExecTimeoutError if timeout elapses and ExecFailedError if the process can’t be spawned.
Parameters
cmdstrCommand to execute (e.g.
“python3”, “/usr/bin/node”).argslist[str] | Mapping[str, Any] | NoneCommand arguments. A mapping is accepted as a shorthand for the keyword-only options below.
cwdstr | NoneWorking directory for this command.
userstr | NoneGuest user to run as.
envMapping[str, str] | NoneEnvironment variables, merged on top of the sandbox defaults.
timeoutfloat | NoneSeconds before the process is killed.
Stdin mode. Raw
bytes / str are sent inline; default is /dev/null.ttyboolAllocate a pseudo-terminal, merging stdout and stderr.
rlimitslist[Rlimit] | NonePOSIX resource limits applied to the process.
Returns
Collected stdout, stderr, and exit status.
sandbox.shell()
Example
Example
/bin/sh). Shell syntax like pipes, redirects, and && chains works. Accepts the same keyword-only options as exec().
Parameters
scriptstrShell command string (e.g.
“ls -la /app && echo done”).cwd, user, env, timeout, stdin, tty, rlimitsSame per-call options as exec().
Returns
Collected stdout, stderr, and exit status.
Stream
sandbox.exec_stream()
Example
Example
ExecHandle that emits stdout, stderr, and exit events as they happen rather than buffering everything. Takes the same per-call options as exec(). Pass stdin=Stdin.pipe() to write to the process while it runs via take_stdin(). For TTY sessions, call resize(rows, cols) when the terminal dimensions change.
Parameters
cmdstrCommand to execute.
argslist[str] | Mapping[str, Any] | NoneCommand arguments.
cwd, user, env, timeout, stdin, tty, rlimitsSame per-call options as exec().
Returns
Streaming handle for receiving events and controlling the process.
sandbox.shell_stream()
Example
Example
shell(): runs script through the configured shell but returns an ExecHandle instead of buffering output.
Parameters
scriptstrShell command string.
cwd, user, env, timeout, stdin, tty, rlimitsSame per-call options as exec().
Returns
Streaming handle.
Attach
sandbox.attach()
Example
Example
Ctrl+]) to disconnect without stopping the process. Returns the process exit code.
Parameters
cmdstrCommand to run.
argslist[str] | NoneCommand arguments.
cwdstr | NoneWorking directory.
userstr | NoneGuest user to run as.
envMapping[str, str] | NoneEnvironment variables for the session.
detach_keysstr | NoneDetach key sequence (e.g.
“ctrl-]” or “ctrl-p,ctrl-q”).Returns
int
Exit code of the process.
sandbox.attach_shell()
Example
Example
Returns
int
Exit code of the shell process.
Types
ExecOutput
Returned by exec() · shell() · ExecHandle.collect()
The result of a completed command execution: collected output plus exit status. All members are properties. In TTY mode,stdout_bytes contains the combined terminal output and stderr_bytes is empty because a PTY doesn’t preserve separate stdout and stderr streams.
ExecHandle
Returned by exec_stream() · shell_stream()
A handle to a running streaming execution. It is an async iterator, soasync for event in handle: yields each ExecEvent until the stream ends. signal(), kill(), and resize() may run while another task is waiting in recv(); concurrent receive, wait, and collect operations are not supported.
ExecSink
Returned by ExecHandle.take_stdin()
Writer for sending data to a running process’s stdin. Obtained fromExecHandle.take_stdin() when the execution was configured with stdin=Stdin.pipe().
ExecEvent
Emitted by ExecHandle
Native event object emitted byrecv() and by iterating an ExecHandle. Fields that don’t apply to a given event are None.
ExitStatus
Used by ExecHandle.wait()
Frozen dataclass describing a process exit result.ExecHandle.wait() returns the same information as a (code, success) tuple.
Stdin
Used by exec() · shell() · exec_stream() · shell_stream()
Frozen dataclass with factory methods for configuring process stdin. Pass the result as thestdin argument to an exec or shell method. Raw bytes and str are also accepted directly and are sent inline.
Rlimit
Used by exec() · shell() · exec_stream() · shell_stream()
Frozen dataclass describing a POSIX resource limit. Construct one directly or via a factory, then pass a list as therlimits argument.
Fields
RlimitResource
Used by Rlimit.resource
String enum (enum.StrEnum) naming a limitable POSIX resource.