Skip to main content

Python SDK for mxlite

Project description

MXLite SDK

PyPI version Python Versions License: Apache-2.0

MXLite SDK 是 MXLite 的官方 Python SDK,为服务器部署和系统管理提供了全面的编程接口。该 SDK 封装了 MXD 服务的核心功能,使开发者能够通过简洁的 API 实现系统安装、网络配置、文件传输和远程命令执行等操作。

特性

  • 全异步 API 设计:支持高效的并发操作,同时提供同步接口以兼容不同的使用场景
  • 自动平台适配:自动检测并使用适合当前平台的二进制文件
  • 多平台支持:兼容 Linux、macOS 和 Windows 等主流操作系统
  • 内置 MXD 服务管理:可以自动启动、监控和关闭 MXD 服务
  • 全面的系统部署工具:提供从系统安装到网络配置的完整工作流
  • 代码类型提示:完善的类型注解支持,提高开发效率

安装

从 PyPI 安装

pip install mxlite-sdk

快速入门

mxlite-sdk 支持连接已启动的 mxd 服务,也支持后台直接运行一个 mxd 服务,默认为使用 MXDRunner 运行一个 mxd 服务并作为子进程使用

基本使用示例(内建服务)

import asyncio
from mxlite import MXLite

async def main():
    # 使用异步上下文管理器自动管理资源
    async with MXLite() as mxc:
        # 启动 MXD 服务,注意,程序会自动获取一个可用的端口,并在此端口提供服务
        mxc.start_mxd()
        
        # 获取主机列表
        hosts, status = await mxlite.get_host_list()
        print(f"找到 {len(hosts.sessions)} 个主机")
        
        if hosts.sessions:
            # 在远程主机上执行命令
            host_id = hosts.sessions[0]  # 使用第一个主机
            result, status = await mxlite.command_exec(host_id, "ls -la")
            task_id = result.task_id
            
            # 等待任务完成
            task_result = await mxlite.until_task_complete(host_id, task_id)
            print(f"命令输出:\n{task_result.payload.payload.stdout}")

if __name__ == "__main__":
    asyncio.run(main())

连接到外部 MXD 服务

如果您已经有一个运行中的 MXD 服务,可以直接连接到它而不是启动新实例:

import asyncio
from mxlite import MXLite

async def main():
    # 创建连接到外部服务的客户端
    client = MXLite(
        host="server-ip-address",  # 外部服务器地址
        http_port=8080,           # MXD HTTP 端口
        token="your-token-here"   # 认证令牌
    )
    
    try:
        # 确保连接成功
        connected = await client.connect_mxd()
        if not connected:
            print("连接到 MXD 服务失败")
            return
        
        # 执行操作...
        hosts, status = await client.get_host_list()
        print(f"成功连接到 MXD 服务,发现 {len(hosts.sessions)} 个主机")
        
    finally:
        # 关闭客户端
        await client.close()

if __name__ == "__main__":
    asyncio.run(main())

使用高级配置启动 MXD

在部分情况下,您可能需要使用 Https、设置指定端口或预置设置一些安全性配置,您可以传入 MXLiteConfig 实例

import asyncio
from mxlite import MXLite, MXLiteConfig

async def main():
    # 创建配置对象
    config = MXLiteConfig(
        root_dir="/path/to/certificates",  # 证书文件所在目录
        http_port=8080,                    # HTTP 端口
        https_port=8443,                   # HTTPS 端口
        token="your-token",                # 认证令牌
        verbose=True                       # 启用详细日志
    )

    async with MXLite(config) as mxc:
        # 启动 MXD 服务,注意,程序会自动获取一个可用的端口,并在此端口提供服务
        mxc.start_mxd()
        
        # 获取主机列表
        hosts, status = await mxlite.get_host_list()
        print(f"找到 {len(hosts.sessions)} 个主机")
        
        if hosts.sessions:
            # 在远程主机上执行命令
            host_id = hosts.sessions[0]  # 使用第一个主机
            result, status = await mxlite.command_exec(host_id, "ls -la")
            task_id = result.task_id
            
            # 等待任务完成
            task_result = await mxlite.until_task_complete(host_id, task_id)
            print(f"命令输出:\n{task_result.payload.payload.stdout}")

if __name__ == "__main__":
    asyncio.run(main())

API 文档

MXLiteConfig

配置类,用于设置 MXLite 客户端参数。

config = MXLiteConfig(
    root_dir=None,         # 根目录,一般为证书文件所在的父级目录,默认为当前目录
    http_port=None,        # HTTP 端口,None 则随机选择
    https_port=None,       # HTTPS 端口,None 则随机选择
    token=None,            # 认证令牌
    certificates_dir=None, # 证书目录
    verbose=False,         # 是否输出详细日志
    host="127.0.0.1"       # 主机地址
)

MXLite

MXLite 客户端类,提供 MXD 服务管理和 API 操作。

# 创建客户端
mxlite = MXLite(
    config=None,             # MXLiteConfig 配置对象
    host=None,               # 外部 MXD 服务主机地址
    http_port=None,          # 外部 MXD 服务 HTTP 端口
    token=None,              # 外部 MXD 服务认证令牌
    auto_connect=True        # 是否自动连接到外部 MXD 服务
)

# 启动和关闭 MXD 服务
mxlite.start_mxd()
mxlite.kill_mxd()

# 连接与断开外部 MXD 服务
mxlite.connect_mxd()
mxlite.disconnect_mxd()

# 主机管理
hosts, status = await mxlite.get_host_list()  # 获取主机列表
host_info, status = await mxlite.get_host_info(host_id)  # 获取主机信息
host_list_info, status = await mxlite.get_host_list_info()  # 获取主机列表详细信息

# 任务管理
task_result, status = await mxlite.get_task_result(host_id, task_id)  # 获取任务结果
result = await mxlite.until_task_complete(host_id, task_id, interval=1)  # 等待任务完成

# 命令执行
result, status = await mxlite.command_exec(host_id, command)  # 在远程主机执行命令

# 文件操作
await mxlite.upload_file(host_id, src_path, target_url)  # 上传文件
await mxlite.download_file(host_id, src_url, target_path)  # 下载文件
await mxlite.add_file_map(file, publish_name)  # 添加文件映射
await mxlite.add_dir_map(dirname, publish_name)  # 添加目录映射
await mxlite.remove_file_map(file)  # 移除文件映射
maps, _ = await mxlite.get_file_map()  # 获取文件映射列表

# 文件系统操作
files, status = await mxlite.lsdir(path)  # 列出目录内容
content, status = await mxlite.read_file(path, max_size)  # 读取文件内容
hash_value = await mxlite.get_file_hash(file, algorithm)  # 获取文件哈希值

# 资源释放
await mxlite.close()  # 异步方式关闭
mxlite.close_sync()  # 同步方式关闭

在本地开发

构建平台特定的 wheel 包

# 准备二进制文件
# 在 mxlite/bin 目录中放置适当的可执行文件,例如:
# - mxd-linux-x86_64, mxa-linux-x86_64         # Linux x86_64
# - mxd-macos-arm64, mxa-macos-arm64           # macOS ARM64
# - mxd-windows-x86_64.exe, mxa-windows-x86_64.exe  # Windows x86_64

# 构建 wheel
python setup.py bdist_wheel

系统要求

  • Python 3.10+
  • 依赖包:
    • aiohttp >= 3.11.18
    • pydantic >= 2.10.0

许可证

本项目采用 Apache 2.0 许可证。详情请参见 LICENSE 文件。

相关链接

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

mxlite_sdk-0.2.1.tar.gz (19.8 MB view details)

Uploaded Source

Built Distributions

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

mxlite_sdk-0.2.1-cp310-none-win_amd64.whl (9.0 MB view details)

Uploaded CPython 3.10Windows x86-64

mxlite_sdk-0.2.1-cp310-none-manylinux2014_x86_64.whl (9.7 MB view details)

Uploaded CPython 3.10

mxlite_sdk-0.2.1-cp310-none-manylinux2014_aarch64.whl (9.5 MB view details)

Uploaded CPython 3.10

mxlite_sdk-0.2.1-cp310-none-macosx_12_0_x86_64.whl (9.4 MB view details)

Uploaded CPython 3.10macOS 12.0+ x86-64

mxlite_sdk-0.2.1-cp310-none-macosx_12_0_arm64.whl (9.3 MB view details)

Uploaded CPython 3.10macOS 12.0+ ARM64

File details

Details for the file mxlite_sdk-0.2.1.tar.gz.

File metadata

  • Download URL: mxlite_sdk-0.2.1.tar.gz
  • Upload date:
  • Size: 19.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for mxlite_sdk-0.2.1.tar.gz
Algorithm Hash digest
SHA256 29059d612a5695b11b8a47e22ea92a31af8741196af661e2307ac4fb39ade392
MD5 0bc6aea48e593e95acf94aa2bc5e8381
BLAKE2b-256 5eec42b5d247b9a37ea28baed8952bd3de846dc1bbf156dedb61130c894b1aff

See more details on using hashes here.

File details

Details for the file mxlite_sdk-0.2.1-cp310-none-win_amd64.whl.

File metadata

  • Download URL: mxlite_sdk-0.2.1-cp310-none-win_amd64.whl
  • Upload date:
  • Size: 9.0 MB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for mxlite_sdk-0.2.1-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 cc41f9bdda60fad6294a2abf628ca92f1f5db7732c3258393b1d894ee823e47c
MD5 c3fda7c4b9131aa2f8b5d1c4457eb8a4
BLAKE2b-256 42ebf5da8b12aea9011d5fad676637fe74348b6806ac76a0ec4449c9bebbe65e

See more details on using hashes here.

File details

Details for the file mxlite_sdk-0.2.1-cp310-none-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for mxlite_sdk-0.2.1-cp310-none-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 20faca99c018d2f562b3c76fad098b72b635dc6567fe6c08b0d0f6777b15ced6
MD5 afdff4aa3f02b1083ca20537cf63ec0c
BLAKE2b-256 88847a4bb277faba710becae56b3bada496496f1812bd472e9ef490f62bff417

See more details on using hashes here.

File details

Details for the file mxlite_sdk-0.2.1-cp310-none-manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for mxlite_sdk-0.2.1-cp310-none-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 22b61770b3bc864779725c5ce452014032ce0c8260032a792a4c6be8d284e8fa
MD5 7a17db3c345a8e7c7420461e38e0e16a
BLAKE2b-256 df1f5ed11f83a596fd700fd2d34ce6446111882860cfbcb135cdf7cbb254098a

See more details on using hashes here.

File details

Details for the file mxlite_sdk-0.2.1-cp310-none-macosx_12_0_x86_64.whl.

File metadata

File hashes

Hashes for mxlite_sdk-0.2.1-cp310-none-macosx_12_0_x86_64.whl
Algorithm Hash digest
SHA256 ddb007a0662eca9454c606833ba2793fd3c17fb296351018938ac998b8b6dc2f
MD5 c903d68d099e68a18ca103c3cf3adeb7
BLAKE2b-256 8263c0c41f502435947b410ea85acf50a3242ea90670e41c581c631cd7059524

See more details on using hashes here.

File details

Details for the file mxlite_sdk-0.2.1-cp310-none-macosx_12_0_arm64.whl.

File metadata

File hashes

Hashes for mxlite_sdk-0.2.1-cp310-none-macosx_12_0_arm64.whl
Algorithm Hash digest
SHA256 b60788046fc3dad96e2dc2d4ebc620a3fe10353d5473ad69288be8a6288cbfc1
MD5 04e3415645e83b94d9e69ae843196c50
BLAKE2b-256 286fdc7ea52b029b26eda9c4808d1c8dd00578fbb55f87924ccc39287c900f51

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