py-lmobile-toolkit
微网通联(51welink)短信服务 Python SDK,提供同步和异步两种发送方式,支持请求签名认证机制。
作者
Guolei
项目主页
- Gitee: https://gitee.com/guolei19850528/py_lmobile_toolkit
- 官方 API 文档: https://www.lmobile.cn/ApiPages/index.html
功能特性
- ✅ 同步短信发送接口
- ✅ 异步短信发送接口
- ✅ 请求签名认证机制(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}")
签名生成算法
签名生成遵循以下步骤:
- 密码 MD5 加密:
MD5(password + smms_encrypt_key),结果转大写 - 参数拼接(按顺序):
AccountId={account_id}&PhoneNos={first_phone}&Password={md5_password}&Random={random}&Timestamp={timestamp} - 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.3.tar.gz
(11.3 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file py_lmobile_toolkit-1.0.3.tar.gz.
File metadata
- Download URL: py_lmobile_toolkit-1.0.3.tar.gz
- Upload date:
- Size: 11.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
uv/0.6.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b8b893da9475e3c753963e4c808bedcf13d9b84307f270a11dcea53c5efd64b0
|
|
| MD5 |
65fefbebb7f3df5951f70991cb429406
|
|
| BLAKE2b-256 |
7b6a79b2047d494d6cbe0dc9d74c1df77ef15248fd73bff8edd12716ff4e6e72
|
File details
Details for the file py_lmobile_toolkit-1.0.3-py3-none-any.whl.
File metadata
- Download URL: py_lmobile_toolkit-1.0.3-py3-none-any.whl
- Upload date:
- Size: 11.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
uv/0.6.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8e4ef07d0111a06b22c5d3afae6e688bafe91c1748fcb56007cf892d1d36c330
|
|
| MD5 |
14f72a30bbe5ebd7aab49bafa7f45db5
|
|
| BLAKE2b-256 |
d48f65d98c26286667d1660b7fed79ba49f2bfaa8c00adbe9740ee9d18d0c17e
|