Python SDK

零第三方依赖 —— 仅标准库 urllib + json + dataclasses,约 300 行。

安装

pip install gateway-new-sdk

初始化

from gateway_new_sdk import Client

c = Client(base_url="https://api.gateway.wechas.com", api_key="ak_xxx.yyy")

方法

方法HTTPcontrols
list_hosts()GET /v1/api/hosts
make_dir(host_id, path)POST .../files/dir1
remove_dir(host_id, path)POST .../files/dir2
exec_shell(host_id, ShellRequest)POST .../process/shell1<<13
exec_program(host_id, path, args)POST .../process/exec1<<10
kill_process(host_id, name, timeout_s)POST .../process/kill1<<12
system_metrics(host_id)POST .../system/metrics1<<25
raw_control(host_id, controls, path, data)POST .../controls/raw任意

错误处理

from gateway_new_sdk import is_host_offline, is_insufficient_balance

try:
    r = c.exec_shell(host_id, ShellRequest(path="powershell.exe"))
except Exception as e:
    if is_insufficient_balance(e):  # 402
        ...
    if is_host_offline(e):           # 409 + error="host_offline"
        ...
    raise

完整示例

from gateway_new_sdk import Client, ShellRequest, is_insufficient_balance

c = Client(base_url="https://api.gateway.wechas.com", api_key="ak_xxx.yyy")

# 列主机
for h in c.list_hosts():
    print(f"{h.name} online={h.online}")

# 性能采样
m = c.system_metrics("<host-uuid>")
print(f"CPU={m.cpu_pct}% Mem={m.mem_pct}% Disk={m.disk_pct}%")

# 跑 PowerShell
try:
    r = c.exec_shell("<host-uuid>", ShellRequest(
        path="powershell.exe",
        args="Get-Process | Select-Object -First 5",
        timeout_s=10,
    ))
except Exception as e:
    if is_insufficient_balance(e):
        print("余额不足")
    raise

print(r.stdout)

源码

完整源码与单测见 sdk-clients/python/