Skip to main content

py-lmobile-toolkit

微网通联(51welink)短信服务 Python SDK,提供同步和异步两种发送方式,支持请求签名认证机制。

作者

Guolei

项目主页

功能特性

  • ✅ 同步短信发送接口
  • ✅ 异步短信发送接口
  • ✅ 请求签名认证机制(SHA256)
  • ✅ 响应数据模型验证(Pydantic)
  • ✅ 完整的工具函数支持

安装

使用 uv 安装:

uv add py-lmobile-toolkit

或从pip安装:

pip install py-lmobile-toolkit

依赖包

依赖 版本要求 说明
pydantic >=2.0 数据验证
py-httpx-toolkit >=1.0.1 HTTP 工具封装

API 接口

Sms 类

短信客户端核心类,提供同步和异步发送接口。

初始化

from py_lmobile_toolkit.sms import Sms

sms = Sms(
    account_id="your_account",
    password="your_password",
    product_id="your_product"
)

参数说明:

参数 类型 默认值 说明
base_url str https://api.51welink.com/ API 服务器地址
account_id str None 账号 ID
password str None 账号密码
product_id str/int None 产品 ID
smms_encrypt_key str SMmsEncryptKey 加密密钥
client_kwargs dict None httpx.Client 配置

同步发送

response = sms.send(
    phone_nos="13800138000",
    content="您的验证码是:123456"
)

异步发送

response = await sms.async_send(
    phone_nos=["13800138000", "13900139000"],
    content="测试短信"
)

参数说明:

参数 类型 说明
phone_nos str/list/tuple 手机号码,支持单个或多个
content str 短信内容
client httpx.Client/AsyncClient 可选的客户端实例
client_kwargs dict 客户端配置

Utils 工具函数

时间戳生成

from py_lmobile_toolkit.sms.utils import timestamp

ts = timestamp()  # 返回毫秒级时间戳

随机数生成

from py_lmobile_toolkit.sms.utils import random_digits

digits = random_digits(length=10)  # 生成10位随机数

SHA256 签名生成

from py_lmobile_toolkit.sms.utils import sha256_signature

signature = sha256_signature(
    account_id="dljtwy00",
    password="g07KjuLN1",
    phone_nos="13800138000",
    random_digits="1234567890",
    timestamp="1630000000000",
    smms_encrypt_key="SMmsEncryptKey"
)

响应模型转换

from py_lmobile_toolkit.sms.responses import build_base_instance

result = build_base_instance(response)  # 转换为 Success 模型

if result.is_success():
    print("短信成功发送")
else:
    print(f"短信发送失败,错误码: {result.Result}")

响应模型

Base

所有响应的基类:

from py_lmobile_toolkit.sms.responses import Base


class Base(BaseModel):
    Result: str  # 错误码,succ 表示成功

使用示例

同步发送示例

from py_lmobile_toolkit.sms import Sms

# 初始化客户端
sms = Sms(
    account_id="your_account",
    password="your_password",
    product_id="your_product"
)

# 发送单条短信
response = sms.send(
    phone_nos="13800138000",
    content="您的验证码是:123456"
)

# 解析响应
data = response.json()
print(f"发送结果: {data.get('Result')}")

异步发送示例

import asyncio
from py_lmobile_toolkit.sms import Sms


async def main():
    # 初始化客户端
    sms = Sms(
        account_id="your_account",
        password="your_password",
        product_id="your_product"
    )

    # 批量发送短信
    response = await sms.async_send(
        phone_nos=["13800138000", "13900139000", "13700137000"],
        content="【通知】您的订单已发货"
    )

    # 解析响应
    data = response.json()
    print(f"发送结果: {data.get('Result')}")


asyncio.run(main())

签名验证示例

from py_lmobile_toolkit.sms.utils import sha256_signature, timestamp, random_digits

# 生成签名所需参数
ts = timestamp()
digits = random_digits()

# 生成签名
signature = sha256_signature(
    account_id="your_account",
    password="your_password",
    phone_nos="13800138000",
    random_digits=digits,
    timestamp=ts,
    smms_encrypt_key="SMmsEncryptKey"
)

print(f"生成的签名: {signature}")

签名生成算法

签名生成遵循以下步骤:

  1. 密码 MD5 加密MD5(password + smms_encrypt_key),结果转大写
  2. 参数拼接(按顺序):
    AccountId={account_id}&PhoneNos={first_phone}&Password={md5_password}&Random={random}&Timestamp={timestamp}
    
  3. SHA256 加密:对拼接后的字符串进行 SHA256 哈希

许可证

MIT License

Download files

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

Source Distribution

py_lmobile_toolkit-1.0.4.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

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

py_lmobile_toolkit-1.0.4-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file py_lmobile_toolkit-1.0.4.tar.gz.

File metadata

  • Download URL: py_lmobile_toolkit-1.0.4.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.11

File hashes

Hashes for py_lmobile_toolkit-1.0.4.tar.gz
Algorithm Hash digest
SHA256 3d95bdb4a70778759c4c208d8894e17aa382ce26fc5e901ae2b14733f3f960e8
MD5 b89cf144e0a6dce1978f5b6ba214a695
BLAKE2b-256 432ef3d008e769260afb44d6dbc70ce72532b21304150d363452940f54b1fdcf

See more details on using hashes here.

File details

Details for the file py_lmobile_toolkit-1.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for py_lmobile_toolkit-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 fa16aa779101a17f427018d8d1ca43deab8076697d7ee9fc6feeb43e2e827a82
MD5 4085fcd906c49d083051888e7ddf37db
BLAKE2b-256 39a4a80cd4451fdf1b3084277dac403631b5aca0ba9c583721eacdf1eccb9284

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.0.4 This release

2 files

1.0.3

2 files

1.0.2

2 files

1.0.1

2 files

1.0.0

2 files

Supported by

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