Skip to main content

UCloud Sandbox Python SDK

UCloud Sandbox Python SDK 提供云端沙箱环境,用于安全运行 AI 生成的代码。

安装

pip install ucloud_sandbox

快速开始

1. 获取 API Key

  1. 访问 星图平台 注册账号
  2. 在 密钥管理 获取 API Key
  3. 设置环境变量:
export UCLOUD_SANDBOX_API_KEY=your_api_key

2. 基础沙箱

from ucloud_sandbox import Sandbox

with Sandbox.create() as sandbox:
    result = sandbox.commands.run("echo 'Hello, World!'")
    print(result.stdout)

3. Code Interpreter(代码解释器)

支持有状态的代码执行,变量在多次调用之间保持:

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']

4. Desktop(桌面环境)

支持鼠标键盘控制、截图、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()

私有化 HTTP 部署

私有化服务未配置 TLS 时,设置 sandbox 域名并启用非安全 HTTP:

export UCLOUD_SANDBOX_DOMAIN=sandbox.example.com
export UCLOUD_SANDBOX_INSECURE_HTTP=true

默认管理 API 地址为 http://api.<domain>;如果实际地址不符合这个规则,再通过 UCLOUD_SANDBOX_API_URL 指定完整 URL。

也可以在调用时传入参数:

from ucloud_sandbox import Sandbox

sandbox = Sandbox.create(
    api_url="http://api.sandbox.example.com",
    domain="sandbox.example.com",
    insecure_http=True,
)

UCLOUD_SANDBOX_INSECURE_HTTP 默认为 false;仅应在可信私有网络中启用。

文档

访问 UCloud Agent Sandbox 文档 获取更多信息。

致谢

本项目基于 E2B 开源项目开发,感谢 E2B 团队的贡献。

许可证

MIT License - 详见 LICENSE 文件

Metadata

Release files for ucloud-sandbox 1.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ucloud-sandbox 1.5.0
File Size Uploaded
ucloud_sandbox-1.5.0.tar.gz 241.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ucloud-sandbox 1.5.0
File Interpreter ABI Platform
ucloud_sandbox-1.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 650.8 kB

Release files / ucloud_sandbox-1.5.0.tar.gz

Download URL ucloud_sandbox-1.5.0.tar.gz
Size 241.4 kB
Tags Source
SHA-256 checksum
How to use checksums
c02b82461f268223a139db4c7c4fe11504525dd07644a74b6f866f563a4db9c3
BLAKE2b-256 checksum
How to use checksums
1dc707b11eafd24eaf7cb12765217867a2224dd140db2042527d53a17fbf6d36
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / ucloud_sandbox-1.5.0-py3-none-any.whl

Download URL ucloud_sandbox-1.5.0-py3-none-any.whl
Size 409.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0bb3a8be624620a47ad1a8207bfdea5c3565a878a396028eab7908f597c1f8bc
BLAKE2b-256 checksum
How to use checksums
3396dec5affa343ec6d425d053af38b4c7817756c4b4015c948263e5e4573506
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release history Release notifications | RSS feed

This release

1.5.0 This release

2 release files

1.4.5

2 release files

1.4.4

2 release files

1.4.3

2 release files

1.4.2

2 release files

1.4.1

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.9

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page