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

依赖包

依赖 版本要求 说明
httpx >=0.27.0 HTTP 客户端
pydantic >=2.0 数据验证
jsonpath-ng >=1.5.3 JSONPath 查询
jsonschema >=4.21.0 JSON Schema 校验
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"
)

JSONPath 查询

from py_lmobile_toolkit.sms.utils import json_find_first

data = {"store": {"books": [{"title": "Python"}, {"title": "Java"}]}}
title = json_find_first("$.store.books[0].title", data)  # 返回 "Python"

JSON Schema 校验

from py_lmobile_toolkit.sms.utils import json_is_valid

schema = {"type": "object", "properties": {"name": {"type": "string"}}}
valid = json_is_valid(schema, {"name": "test"})  # 返回 True

响应模型转换

from py_lmobile_toolkit.sms.utils import build_success_instance

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

响应模型

Base

所有响应的基类:

from py_lmobile_toolkit.sms.responses import Base

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

Success

成功响应模型:

from py_lmobile_toolkit.sms.responses import Success

result = Success(Result="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.0.tar.gz (13.0 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.0-py3-none-any.whl (13.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for py_lmobile_toolkit-1.0.0.tar.gz
Algorithm Hash digest
SHA256 f0a3703124f334cce081cb3f3e62169f300cc6ac1925c98928465085a33a7d37
MD5 5ceef5647dec94fd11504c4adf0644e5
BLAKE2b-256 1ef03a9a195e69475b93972a1590cc7b4fbabcba00221afdedc308182cb08aa5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for py_lmobile_toolkit-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 74a1043d659aeab06f64164f67a351f953bbd1aa2d624c583e12919539b1e67a
MD5 ce90298ab7e3040d3320e3dbd60f2456
BLAKE2b-256 b14ff3dec832f6ebb2073cb8e8ec2be6937557b0d894b1d8d551ccb45a653166

See more details on using hashes here.

Release history Release notifications | RSS feed

1.0.4

2 files

1.0.3

2 files

1.0.2

2 files

1.0.1

2 files

This release

1.0.0 This release

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