# hello.py: create an OpenSandbox sandbox, run Python in it, delete it. # On the VM: python hello.py # From elsewhere: OPEN_SANDBOX_DOMAIN=https://.exe.xyz:8080 EXE_VM_TOKEN=... python hello.py import asyncio, os, time from datetime import timedelta from opensandbox import Sandbox from opensandbox.config import ConnectionConfig from opensandbox.models import WriteEntry domain = os.environ.get("OPEN_SANDBOX_DOMAIN", "http://localhost:8080") headers = {} if os.environ.get("EXE_VM_TOKEN"): # needed only through the exe.dev proxy headers["X-Exedev-Authorization"] = "Bearer " + os.environ["EXE_VM_TOKEN"] config = ConnectionConfig( domain=domain, protocol="https" if domain.startswith("https://") else "http", api_key=os.environ["OPEN_SANDBOX_API_KEY"], headers=headers, ) async def main(): start = time.time() sandbox = await Sandbox.create( "python:3.12-slim", connection_config=config, timeout=timedelta(minutes=10) ) print(f"sandbox {sandbox.id} ready in {time.time() - start:.1f}s") try: await sandbox.files.write_files( [WriteEntry(path="/tmp/hello.py", data="import sys\nprint('hello from Python', sys.version.split()[0])\n")] ) run = await sandbox.commands.run("python3 /tmp/hello.py && uname -r") for line in run.logs.stdout: print(line.text.rstrip()) finally: await sandbox.kill() print("deleted") asyncio.run(main())