UCloud Sandbox Python SDK 提供云端沙箱环境,用于安全运行 AI 生成的代码。
pip install ucloud_sandbox- 访问 UCloud Sandbox 注册账号
- 在控制台获取 API Key
- 设置环境变量:
export UCLOUD_SANDBOX_API_KEY=your_api_keyfrom ucloud_sandbox import Sandbox
with Sandbox.create() as sandbox:
result = sandbox.commands.run("echo 'Hello, World!'")
print(result.stdout)支持有状态的代码执行,变量在多次调用之间保持:
from ucloud_sandbox.code_interpreter import Sandbox
with Sandbox.create() as sandbox:
sandbox.run_code("x = 1")
execution = sandbox.run_code("x += 1; print(x)")
print(execution.logs.stdout) # ['2']支持鼠标键盘控制、截图、VNC 流媒体:
from ucloud_sandbox.desktop import Sandbox
desktop = Sandbox.create()
# 截图
screenshot = desktop.screenshot()
# 鼠标操作
desktop.left_click(100, 200)
desktop.write("Hello, World!")
# VNC 流
desktop.stream.start()
print(desktop.stream.get_url())
desktop.kill()TODO 访问 Sandbox 文档 获取更多信息。
本项目基于 E2B 开源项目开发,感谢 E2B 团队的贡献。
MIT License - 详见 LICENSE 文件