Skip to main content

Python SDK for Kanyun Sandbox Platform - Control Plane client with integrated Agent access

Project description

Kanyun Sandbox Python SDK

Kanyun Sandbox 平台的 Python SDK,提供 Control Plane 客户端和集成的 Agent 访问能力。

安装

pip install kanyun-sandbox

快速开始

from kanyun_sandbox import Client

client = Client(
    control_plane_url="https://sandbox.zhenguanyu.com",
    api_key="your-api-key",
)

# 使用 run_session 自动管理 Sandbox 生命周期
with client.run_session(template_name="python", ttl_seconds=300) as session:
    # 执行 Shell 命令
    result = session.agent.shell.exec_command(command="echo hello")
    print(result.data.output)

    # 写入文件
    session.agent.file.write_file(file="/tmp/test.txt", content="Hello!")

    # 读取文件
    read_result = session.agent.file.read_file(file="/tmp/test.txt")
    print(read_result.data.content)
# Sandbox 在 with 块结束时自动销毁

客户端配置

client = Client(
    control_plane_url="https://sandbox.example.com",  # 必填
    api_key="your-api-key",          # API 认证密钥
    user_id="user-123",              # 用户 ID(X-User-ID header)
    agent_port=8080,                 # Agent 端口(默认 8080)
    use_agent_endpoint=False,        # 优先使用 agent_endpoint 而非 pod_ip
    timeout=60.0,                    # HTTP 请求超时(秒)
)

API

Sandbox 管理

from kanyun_sandbox import CreateSandboxRequest

# 创建 Sandbox
handle = client.create_sandbox(CreateSandboxRequest(
    template_name="python",
    ttl_seconds=600,
    labels={"env": "test"},
))

# 通过 handle 执行命令
result = handle.agent.shell.exec_command(command="ls -la")

# 延长 TTL
handle.extend(ttl_seconds=300)

# 销毁
handle.destroy()
# 获取 Sandbox 信息
info = client.get_sandbox("sandbox-id")

# 列出 Sandbox
response = client.list_sandboxes(label_selector="env=test")
for sb in response.sandboxes:
    print(f"{sb.id}: {sb.phase}")

# 连接已有 Sandbox
handle = client.connect_sandbox("sandbox-id")

# 删除 Sandbox
client.delete_sandbox("sandbox-id")

Template 管理

# 列出所有模板
templates = client.list_templates()
for t in templates.templates:
    print(f"{t.name}: {t.description}")

# 获取指定模板
tmpl = client.get_template("python")

Session(推荐)

# 方式 1: context manager(推荐)
with client.run_session(template_name="python", ttl_seconds=300) as session:
    session.agent.shell.exec_command(command="echo hello")

# 方式 2: 手动管理
session = client.new_session(CreateSandboxRequest(template_name="python"))
try:
    session.agent.shell.exec_command(command="echo hello")
finally:
    session.close()

Agent 能力

通过 session.agenthandle.agent 访问 agent-sandbox 提供的全部能力:

  • Shell: agent.shell.exec_command(...) - 执行 Shell 命令
  • File: agent.file.read_file(...), agent.file.write_file(...), agent.file.list_path(...) - 文件操作
  • Jupyter: agent.jupyter - Jupyter notebook 执行
  • Node.js: agent.nodejs - Node.js 代码执行
  • Browser: agent.browser - 浏览器自动化
  • Code: agent.code - 统一代码执行

与 Go SDK 对照

Go SDK Python SDK
kanyun.NewClient(opts...) Client(control_plane_url=..., api_key=...)
client.CreateSandbox(ctx, req) client.create_sandbox(req)
client.RunSession(ctx, req, fn) with client.run_session(...) as session:
handle.Agent().Shell.ExecCommand(...) handle.agent.shell.exec_command(...)
handle.Extend(ctx, ttl) handle.extend(ttl)
handle.Destroy(ctx) handle.destroy()
session.Close(ctx) session.close()

依赖

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kanyun_sandbox-0.1.0.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kanyun_sandbox-0.1.0-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file kanyun_sandbox-0.1.0.tar.gz.

File metadata

  • Download URL: kanyun_sandbox-0.1.0.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.1

File hashes

Hashes for kanyun_sandbox-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d08ac8613f8511610175f9878e17209e4672b2c0a4e15b41363489db327ff0eb
MD5 4f23d3df03d7a2e1c86970a8c1de1666
BLAKE2b-256 9327a989f9650edc6b6a06788817b12377aefb75580ae42536b7cd945cc646c2

See more details on using hashes here.

File details

Details for the file kanyun_sandbox-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: kanyun_sandbox-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.1

File hashes

Hashes for kanyun_sandbox-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 326c98b0d3167d2172ddecbb09e6d4da79dca55c3c16c3ff6d5d6e627fa8c255
MD5 2bb3e3739711f9eac73c395f1491a524
BLAKE2b-256 fcce0254d96bf166f3094290571e0c8b7dfae6ca84728d6760717daf69cae237

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page