Represents an active sandbox for running commands and file operations.
This class is typically obtained from SandboxClient.sandbox() and supports the context manager protocol for automatic cleanup.
Sandbox(
self,
name: str,
template_name: str,
dataplane_url: Optional[str] = None,
id: Optional[str] = None,
status: str = 'ready',
status_message: Optional[str] = None,
created_at: Optional[str] = None,
updated_at: Optional[str] = None,
ttl_seconds: Optional[int] = None,
idle_ttl_seconds: Optional[int] = None,
expires_at: Optional[str] = None,
_client: SandboxClient = None,
_auto_delete: bool = True
)Example:
with client.sandbox(template_name="python-sandbox") as sandbox: result = sandbox.run("python --version") print(result.stdout)
Create a Sandbox from API response dict.
Execute a command in the sandbox.
Reconnect to a running or recently-finished command.
Resumes output from the given byte offsets. Any output produced while the client was disconnected is replayed from the server's ring buffer.
Write content to a file in the sandbox.
Read a file from the sandbox.
Open a TCP tunnel to a port inside the sandbox.
Creates a local TCP listener that forwards connections through a yamux-multiplexed WebSocket to the specified port inside the sandbox. Works with any TCP protocol (databases, Redis, HTTP, etc.).
Use as a context manager for automatic cleanup::
with sandbox.tunnel(remote_port=5432) as t:
conn = psycopg2.connect(host="127.0.0.1", port=t.local_port)
Or manage the lifecycle explicitly::
t = sandbox.tunnel(remote_port=5432)
# ... use tunnel ...
t.close()