一个用于与微网通联 API 交互的 Python 客户端库
Project description
py-lmobile
微网通联短信服务 Python SDK
简介
py_lmobile 是一个用于接入微网通联(51welink)短信服务的 Python SDK,提供同步和异步两种发送方式,支持请求签名认证。
安装
pip install py_lmobile
快速开始
同步发送短信
from py_lmobile.sms import Sms
# 初始化客户端
sms = Sms(
base_url="https://api.51welink.com/",
account_id="your_account_id",
password="your_password",
product_id="your_product_id"
)
# 发送短信
response = sms.send(
phone_nos="13800138000",
content="您的验证码是:123456"
)
# 处理响应
print(response.json())
异步发送短信
import asyncio
from py_lmobile.sms import Sms
async def main():
# 初始化客户端
sms = Sms(
base_url="https://api.51welink.com/",
account_id="your_account_id",
password="your_password",
product_id="your_product_id"
)
# 异步发送短信
response = await sms.async_send(
phone_nos=["13800138000", "13900139000"],
content="您的验证码是:123456"
)
# 处理响应
print(response.json())
asyncio.run(main())
API 文档
Sms 类
初始化参数
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
| base_url | str | 否 | https://api.51welink.com/ |
API服务器地址 |
| account_id | str | 是 | - | 账号ID |
| password | str | 是 | - | 密码 |
| product_id | str/int | 是 | - | 产品ID |
| smms_encrypt_key | str | 否 | SMmsEncryptKey |
加密密钥 |
| client_kwargs | dict | 否 | - | 传递给httpx.Client的额外配置 |
方法
send() - 同步发送短信
def send(
client: Optional[httpx.Client] = None,
phone_nos: Optional[Union[str, list, tuple]] = None,
content: Optional[str] = None,
**kwargs
) -> httpx.Response
参数:
| 参数 | 类型 | 说明 |
|---|---|---|
| client | httpx.Client | 可选的HTTP客户端实例 |
| phone_nos | str/list/tuple | 接收短信的手机号码 |
| content | str | 短信内容 |
| **kwargs | - | 传递给httpx.Client.request的额外参数 |
返回: httpx.Response - HTTP响应对象
async_send() - 异步发送短信
async def async_send(
client: Optional[httpx.AsyncClient] = None,
phone_nos: Optional[Union[str, list, tuple]] = None,
content: Optional[str] = None,
**kwargs
) -> httpx.Response
参数:
| 参数 | 类型 | 说明 |
|---|---|---|
| client | httpx.AsyncClient | 可选的异步HTTP客户端实例 |
| phone_nos | str/list/tuple | 接收短信的手机号码 |
| content | str | 短信内容 |
| **kwargs | - | 传递给httpx.AsyncClient.request的额外参数 |
返回: httpx.Response - HTTP响应对象
工具函数
timestamp()
生成当前时间戳(毫秒)
from py_lmobile.sms.utils import timestamp
ts = timestamp() # 返回示例: 1630000000000
random_digits(length: int = 10)
生成指定长度的随机数字
from py_lmobile.sms.utils import random_digits
digits = random_digits(10) # 返回示例: 1234567890
sha256_signature()
生成SHA256签名,用于请求认证
from py_lmobile.sms.utils import sha256_signature
signature = sha256_signature(
account_id="your_account_id",
password="your_password",
phone_nos="13800138000",
random_digits="1234567890",
timestamp="1630000000000",
smms_encrypt_key="SMmsEncryptKey"
)
convert_to_result_eq_succ()
将HTTP响应转换为标准化的响应模型
from py_lmobile.sms.utils import convert_to_result_eq_succ
response = sms.send(phone_nos="13800138000", content="test")
result = convert_to_result_eq_succ(response)
print(result.Result) # "succ"
result_eq_succ_validator()
校验响应是否符合成功响应的JSON Schema
from py_lmobile.sms.utils import result_eq_succ_validator
response = sms.send(phone_nos="13800138000", content="test")
is_valid = result_eq_succ_validator(response) # True 或 False
响应模型
RESULT_EQ_SUCC
成功响应模型,继承自Base模型
from py_lmobile.sms.responses import RESULT_EQ_SUCC
result = RESULT_EQ_SUCC(Result="succ")
print(result.Result) # "succ"
配置说明
httpx 客户端配置
可以通过 client_kwargs 参数自定义 httpx 客户端配置:
sms = Sms(
account_id="your_account_id",
password="your_password",
product_id="your_product_id",
client_kwargs={
"timeout": 30,
"verify": True,
"proxies": "http://proxy:8080"
}
)
注意事项
- 密码会在内部进行MD5加密后参与签名计算
- 签名算法使用SHA256,按照API要求的参数顺序拼接
- 多个手机号请使用逗号分隔或传入列表
- 建议复用HTTP客户端以提高性能
许可证
MIT License
主页
https://gitee.com/guolei19850528/py_lmobile
作者
Guolei174000902@qq.com
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
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-0.1.0.tar.gz.
File metadata
- Download URL: py_lmobile-0.1.0.tar.gz
- Upload date:
- Size: 9.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8f377b6cbcd5301877ac07821645f6743f29c2ac12b7fa9d96a733c396d45706
|
|
| MD5 |
879868fbbdc140ba3a2da74ab0281bcc
|
|
| BLAKE2b-256 |
a8a793b1b3a6ab2d06e5e645303c058e97e1b1a3a718a8a1c39af842a051f32a
|
File details
Details for the file py_lmobile-0.1.0-py3-none-any.whl.
File metadata
- Download URL: py_lmobile-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1a89909801ed2b533a74832ed0426700373c441461f502c4859f1e7f7906b4f6
|
|
| MD5 |
2ce43a88528655cb175d4a94065a3b9b
|
|
| BLAKE2b-256 |
aa7c251882f2d210e66f47c2a3dc715bd1329f7ba3e562d9b05e3a14ef149f21
|