Skip to main content

mtmqujing

一个用于调用远程服务的Python客户端库,支持同步和异步两种调用方式。

安装

基础安装(仅同步功能):

pip install mtmqujing

包含异步功能的完整安装:

pip install mtmqujing[async]

或者单独安装异步依赖:

pip install aiohttp>=3.8.0

使用方法

同步版本

from mtmqujing import QujingByHttp

# 创建客户端
client = QujingByHttp(host="localhost", port_config=61000)

# 获取包的进程ID
packages = ["your_package"]
ports = client.get_pid(packages)
print(f"端口信息: {ports}")

# 设置目标应用
result = client.set_app(packages)
print(f"设置结果: {result}")

# 调用远程函数
if ports:
    port = list(ports.values())[0]
    data = {"function": "your_function", "args": {"param": "value"}}
    response = client.invoke(port, data)
    print(f"调用结果: {response}")

异步版本

import asyncio
from mtmqujing import AsyncQujingByHttp

async def main():
    # 创建异步客户端
    client = AsyncQujingByHttp(host="localhost", port_config=61000)
    
    # 异步获取包的进程ID
    packages = ["your_package"]
    ports = await client.get_pid(packages)
    print(f"端口信息: {ports}")
    
    # 异步设置目标应用
    result = await client.set_app(packages)
    print(f"设置结果: {result}")
    
    # 异步调用远程函数
    if ports:
        port = list(ports.values())[0]
        data = {"function": "your_function", "args": {"param": "value"}}
        response = await client.invoke(port, data)
        print(f"调用结果: {response}")

# 运行异步函数
asyncio.run(main())

并发调用(异步版本的优势)

import asyncio
from mtmqujing import AsyncQujingByHttp

async def concurrent_calls():
    client = AsyncQujingByHttp(host="localhost", port_config=61000)
    
    # 同时发起多个异步调用
    packages_list = [["package1"], ["package2"], ["package3"]]
    tasks = [client.get_pid(packages) for packages in packages_list]
    results = await asyncio.gather(*tasks)
    
    print(f"并发调用结果: {results}")

asyncio.run(concurrent_calls())

API 文档

QujingByHttp (同步版本)

__init__(host, port_config=61000, protocol="http")

初始化客户端。

  • host: 服务器地址
  • port_config: 配置端口,默认61000
  • protocol: 协议,默认"http"

get_pid(packages, **kwargs)

获取包名对应的进程号(端口号)。

  • packages: 包名列表
  • 返回: 字典,包名到端口号的映射

set_app(packages, **kwargs)

设置目标应用。

  • packages: 包名列表
  • 返回: 布尔值,表示是否设置成功

invoke(port_invoke, data, **kwargs)

调用目标应用的函数。

  • port_invoke: 调用端口
  • data: 调用数据
  • 返回: 字典,包含响应数据和数据类型

AsyncQujingByHttp (异步版本)

异步版本的API与同步版本完全相同,只是所有方法都是异步的(需要使用await关键字)。

数据转换工具

库还提供了FormatConvert类用于各种数据格式转换:

from mtmqujing.convert import FormatConvert

# Base64编码/解码
encoded = FormatConvert.bytes2base64(b"hello")
decoded = FormatConvert.base642bytes(encoded)

# JSON与字符串转换
json_str = FormatConvert.json2str({"key": "value"})
data = FormatConvert.str2json(json_str)

# Base64与JSON转换
b64_json = FormatConvert.json2base64({"key": "value"})
json_data = FormatConvert.base642json(b64_json)

# Gzip压缩相关转换
compressed = FormatConvert.gzip_json_to_base64({"key": "value"})
decompressed = FormatConvert.ungzip_base64_to_json(compressed)

错误处理

当调用失败时,会抛出QujingInvokeError异常:

from mtmqujing.exceptions import QujingInvokeError

try:
    response = client.invoke(port, data)
except QujingInvokeError as e:
    print(f"调用失败: {e}")

许可证

MIT License

Metadata

Release files for mtmqujing 0.2.2

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

Source distribution (sdist)

Source distribution for mtmqujing 0.2.2
File Size Uploaded Provenance Actions
mtmqujing-0.2.2.tar.gz 44.3 kB Without provenance Details for mtmqujing-0.2.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mtmqujing 0.2.2
File Interpreter ABI Platform Actions
mtmqujing-0.2.2-py3-none-any.whl Python 3 none any Details for mtmqujing-0.2.2-py3-none-any.whl

Total release size: 73.7 kB

Release files / mtmqujing-0.2.2.tar.gz

Download URL mtmqujing-0.2.2.tar.gz
Size 44.3 kB
Tags Source
SHA-256 checksum
How to use checksums
ab349c27b329b4b669abccdac4a0f60e1920cdb3e23bd38f440201f8df18e725
BLAKE2b-256 checksum
How to use checksums
6bf17eeb72f62f5a87f9258acd75f3704ac89e6d0ccd3db90fa7c2d7165da1a4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / mtmqujing-0.2.2-py3-none-any.whl

Download URL mtmqujing-0.2.2-py3-none-any.whl
Size 29.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f425d14c3e9ba348a6d66e33bcdcaeb519540b6a3d93264d541b46982df8055e
BLAKE2b-256 checksum
How to use checksums
c18a7ca3490c941056154cf0db7c67c3dd384b603168622eb0f4081b971fc61b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Security for mtmqujing 0.2.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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