2026-01-16 16:17:58 +00:00
|
|
|
from daytona import (
|
|
|
|
|
CreateSandboxFromImageParams,
|
|
|
|
|
Daytona,
|
|
|
|
|
DaytonaTimeoutError,
|
|
|
|
|
ExecutionError,
|
|
|
|
|
OutputMessage,
|
|
|
|
|
Resources,
|
|
|
|
|
Sandbox,
|
|
|
|
|
)
|
2025-05-23 13:53:44 +02:00
|
|
|
|
2025-05-29 14:20:03 +02:00
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
daytona = Daytona()
|
|
|
|
|
|
2025-06-05 19:36:53 +02:00
|
|
|
params = CreateSandboxFromImageParams(
|
|
|
|
|
image="python:3.9.23-slim",
|
2025-05-29 14:20:03 +02:00
|
|
|
language="python",
|
2025-06-05 19:36:53 +02:00
|
|
|
resources=Resources(
|
|
|
|
|
cpu=1,
|
|
|
|
|
memory=1,
|
|
|
|
|
disk=3,
|
|
|
|
|
),
|
2025-05-29 14:20:03 +02:00
|
|
|
)
|
2025-06-05 19:36:53 +02:00
|
|
|
sandbox = daytona.create(params, timeout=150, on_snapshot_create_logs=print)
|
2025-05-29 14:20:03 +02:00
|
|
|
|
|
|
|
|
# Run the code securely inside the sandbox
|
|
|
|
|
response = sandbox.process.code_run('print("Hello World!")')
|
|
|
|
|
if response.exit_code != 0:
|
|
|
|
|
print(f"Error: {response.exit_code} {response.result}")
|
|
|
|
|
else:
|
|
|
|
|
print(response.result)
|
|
|
|
|
|
|
|
|
|
# Execute an os command in the sandbox
|
2025-06-05 19:36:53 +02:00
|
|
|
response = sandbox.process.exec('echo "Hello World from exec!"', timeout=10)
|
2025-05-29 14:20:03 +02:00
|
|
|
if response.exit_code != 0:
|
|
|
|
|
print(f"Error: {response.exit_code} {response.result}")
|
|
|
|
|
else:
|
|
|
|
|
print(response.result)
|
|
|
|
|
|
2025-12-01 14:08:57 +01:00
|
|
|
stateful_code_interpreter(sandbox)
|
|
|
|
|
|
2025-05-29 14:20:03 +02:00
|
|
|
daytona.delete(sandbox)
|
|
|
|
|
|
|
|
|
|
|
2025-12-01 14:08:57 +01:00
|
|
|
def stateful_code_interpreter(sandbox: Sandbox):
|
|
|
|
|
def handle_stdout(message: OutputMessage):
|
|
|
|
|
print(f"[STDOUT] {message.output}")
|
|
|
|
|
|
|
|
|
|
def handle_stderr(message: OutputMessage):
|
|
|
|
|
print(f"[STDERR] {message.output}")
|
|
|
|
|
|
|
|
|
|
def handle_error(error: ExecutionError):
|
|
|
|
|
print(f"[ERROR] {error.name}: {error.value}\n{error.traceback}")
|
|
|
|
|
|
|
|
|
|
print("\n" + "=" * 60)
|
|
|
|
|
print("Stateful Code Interpreter")
|
|
|
|
|
print("=" * 60)
|
|
|
|
|
|
|
|
|
|
print("=" * 10 + " Statefulness in the default context " + "=" * 10)
|
|
|
|
|
result = sandbox.code_interpreter.run_code("counter = 1\nprint(f'Initialized counter = {counter}')")
|
|
|
|
|
print(f"[STDOUT] {result.stdout}")
|
|
|
|
|
|
|
|
|
|
result = sandbox.code_interpreter.run_code(
|
|
|
|
|
"counter += 1\nprint(f'Counter after second call = {counter}')",
|
|
|
|
|
on_stdout=handle_stdout,
|
|
|
|
|
on_stderr=handle_stderr,
|
|
|
|
|
on_error=handle_error,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
print("=" * 10 + " Context isolation " + "=" * 10)
|
|
|
|
|
ctx = sandbox.code_interpreter.create_context()
|
|
|
|
|
try:
|
|
|
|
|
ctx_result = sandbox.code_interpreter.run_code(
|
|
|
|
|
"value = 'stored in isolated context'\nprint(f'Isolated context value: {value}')",
|
|
|
|
|
context=ctx,
|
|
|
|
|
on_stdout=handle_stdout,
|
|
|
|
|
on_stderr=handle_stderr,
|
|
|
|
|
on_error=handle_error,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
print("-" * 3 + " Print value from same context " + "-" * 3)
|
|
|
|
|
ctx_result = sandbox.code_interpreter.run_code(
|
|
|
|
|
"print(f'Value still available: {value}')",
|
|
|
|
|
context=ctx,
|
|
|
|
|
)
|
|
|
|
|
print(f"[STDOUT] {ctx_result.stdout}")
|
|
|
|
|
|
|
|
|
|
print("-" * 3 + " Print value from different context " + "-" * 3)
|
2026-01-20 15:43:03 +01:00
|
|
|
_ = sandbox.code_interpreter.run_code(
|
2025-12-01 14:08:57 +01:00
|
|
|
"print(value)",
|
|
|
|
|
on_stdout=handle_stdout,
|
|
|
|
|
on_stderr=handle_stderr,
|
|
|
|
|
on_error=handle_error,
|
|
|
|
|
)
|
|
|
|
|
finally:
|
|
|
|
|
sandbox.code_interpreter.delete_context(ctx)
|
|
|
|
|
|
|
|
|
|
print("=" * 10 + " Timeout handling " + "=" * 10)
|
|
|
|
|
try:
|
|
|
|
|
code = """
|
|
|
|
|
import time
|
|
|
|
|
print('Starting long running task...')
|
|
|
|
|
time.sleep(5)
|
|
|
|
|
print('Finished!')
|
|
|
|
|
"""
|
2026-01-20 15:43:03 +01:00
|
|
|
_ = sandbox.code_interpreter.run_code(
|
2025-12-01 14:08:57 +01:00
|
|
|
code,
|
|
|
|
|
timeout=1,
|
|
|
|
|
on_stdout=handle_stdout,
|
|
|
|
|
on_stderr=handle_stderr,
|
|
|
|
|
on_error=handle_error,
|
|
|
|
|
)
|
|
|
|
|
except DaytonaTimeoutError as exc:
|
|
|
|
|
print(f"Timed out as expected: {exc}")
|
|
|
|
|
|
|
|
|
|
|
2025-05-29 14:20:03 +02:00
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|