Skip to main content

一个用于与微网通联 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"
    }
)

注意事项

  1. 密码会在内部进行MD5加密后参与签名计算
  2. 签名算法使用SHA256,按照API要求的参数顺序拼接
  3. 多个手机号请使用逗号分隔或传入列表
  4. 建议复用HTTP客户端以提高性能

许可证

MIT License

主页

https://gitee.com/guolei19850528/py_lmobile

作者

Guolei174000902@qq.com

Project details


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-0.1.1.tar.gz (9.6 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-0.1.1-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file py_lmobile-0.1.1.tar.gz.

File metadata

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

File hashes

Hashes for py_lmobile-0.1.1.tar.gz
Algorithm Hash digest
SHA256 a81b6888160bc3a9449eb5ff32fcbe51f32e04214183b89b90ecb745433f3e07
MD5 697c6f28c33c1290b2e0f01884f21e3c
BLAKE2b-256 f19c7dacc264c7f1b2be5ed0f18ca0a3d1f074b3c5ae0d1564d43e799e162315

See more details on using hashes here.

File details

Details for the file py_lmobile-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for py_lmobile-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6f3b91223f4bf6e33f33e68bdc4984e1015cf8c0087c89868719369fb2aab002
MD5 6e763c8837a004b66378c41913037647
BLAKE2b-256 d9131cbef826e81823600065d230b8b6b9794ae7a8acf811c031fe67d2f2c6c1

See more details on using hashes here.

Supported by

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