2025-05-29 14:20:03 +02:00
|
|
|
import asyncio
|
|
|
|
|
|
2025-06-11 12:29:47 +00:00
|
|
|
from daytona import AsyncDaytona, CreateSandboxFromImageParams, Resources
|
2025-05-29 14:20:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async def main():
|
|
|
|
|
async with AsyncDaytona() as 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 = await 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 = await 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 = await 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)
|
|
|
|
|
|
|
|
|
|
await daytona.delete(sandbox)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
asyncio.run(main())
|