py-lmobile 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 郭磊
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,290 @@
1
+ Metadata-Version: 2.4
2
+ Name: py-lmobile
3
+ Version: 0.1.0
4
+ Summary: 一个用于与微网通联 API 交互的 Python 客户端库
5
+ Author-email: Guolei <174000902@qq.com>
6
+ License: MIT License
7
+
8
+ Copyright (c) 2026 郭磊
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+
28
+ Project-URL: Homepage, https://gitee.com/guolei19850528/py_lmobile
29
+ Project-URL: Repository, https://gitee.com/guolei19850528/py_lmobile.git
30
+ Keywords: lmobile,python,client,api,微网通联,短信
31
+ Classifier: License :: OSI Approved :: MIT License
32
+ Classifier: Development Status :: 4 - Beta
33
+ Classifier: Intended Audience :: Developers
34
+ Classifier: Programming Language :: Python :: 3
35
+ Classifier: Programming Language :: Python :: 3.10
36
+ Classifier: Programming Language :: Python :: 3.11
37
+ Classifier: Programming Language :: Python :: 3.12
38
+ Classifier: Programming Language :: Python :: 3.13
39
+ Classifier: Operating System :: OS Independent
40
+ Requires-Python: >=3.10
41
+ Description-Content-Type: text/markdown
42
+ License-File: LICENSE
43
+ Requires-Dist: httpx>=0.27.0
44
+ Requires-Dist: pydantic>=2.0
45
+ Requires-Dist: jsonpath-ng>=1.5.3
46
+ Requires-Dist: jsonschema>=4.21.0
47
+ Provides-Extra: dev
48
+ Requires-Dist: pytest>=7.0; extra == "dev"
49
+ Requires-Dist: pytest-cov>=4.0; extra == "dev"
50
+ Requires-Dist: setuptools>=61.0; extra == "dev"
51
+ Requires-Dist: twine>=4.0; extra == "dev"
52
+ Dynamic: license-file
53
+
54
+ # py-lmobile
55
+
56
+ 微网通联短信服务 Python SDK
57
+
58
+ ## 简介
59
+
60
+ `py_lmobile` 是一个用于接入微网通联(51welink)短信服务的 Python SDK,提供同步和异步两种发送方式,支持请求签名认证。
61
+
62
+ ## 安装
63
+
64
+ ```bash
65
+ pip install py_lmobile
66
+ ```
67
+
68
+ ## 快速开始
69
+
70
+ ### 同步发送短信
71
+
72
+ ```python
73
+ from py_lmobile.sms import Sms
74
+
75
+ # 初始化客户端
76
+ sms = Sms(
77
+ base_url="https://api.51welink.com/",
78
+ account_id="your_account_id",
79
+ password="your_password",
80
+ product_id="your_product_id"
81
+ )
82
+
83
+ # 发送短信
84
+ response = sms.send(
85
+ phone_nos="13800138000",
86
+ content="您的验证码是:123456"
87
+ )
88
+
89
+ # 处理响应
90
+ print(response.json())
91
+ ```
92
+
93
+ ### 异步发送短信
94
+
95
+ ```python
96
+ import asyncio
97
+ from py_lmobile.sms import Sms
98
+
99
+ async def main():
100
+ # 初始化客户端
101
+ sms = Sms(
102
+ base_url="https://api.51welink.com/",
103
+ account_id="your_account_id",
104
+ password="your_password",
105
+ product_id="your_product_id"
106
+ )
107
+
108
+ # 异步发送短信
109
+ response = await sms.async_send(
110
+ phone_nos=["13800138000", "13900139000"],
111
+ content="您的验证码是:123456"
112
+ )
113
+
114
+ # 处理响应
115
+ print(response.json())
116
+
117
+ asyncio.run(main())
118
+ ```
119
+
120
+ ## API 文档
121
+
122
+ ### Sms 类
123
+
124
+ #### 初始化参数
125
+
126
+ | 参数 | 类型 | 必填 | 默认值 | 说明 |
127
+ |------|------|------|--------|------|
128
+ | base_url | str | 否 | `https://api.51welink.com/` | API服务器地址 |
129
+ | account_id | str | 是 | - | 账号ID |
130
+ | password | str | 是 | - | 密码 |
131
+ | product_id | str/int | 是 | - | 产品ID |
132
+ | smms_encrypt_key | str | 否 | `SMmsEncryptKey` | 加密密钥 |
133
+ | client_kwargs | dict | 否 | - | 传递给httpx.Client的额外配置 |
134
+
135
+ #### 方法
136
+
137
+ ##### `send()` - 同步发送短信
138
+
139
+ ```python
140
+ def send(
141
+ client: Optional[httpx.Client] = None,
142
+ phone_nos: Optional[Union[str, list, tuple]] = None,
143
+ content: Optional[str] = None,
144
+ **kwargs
145
+ ) -> httpx.Response
146
+ ```
147
+
148
+ **参数**:
149
+
150
+ | 参数 | 类型 | 说明 |
151
+ |------|------|------|
152
+ | client | httpx.Client | 可选的HTTP客户端实例 |
153
+ | phone_nos | str/list/tuple | 接收短信的手机号码 |
154
+ | content | str | 短信内容 |
155
+ | **kwargs | - | 传递给httpx.Client.request的额外参数 |
156
+
157
+ **返回**: `httpx.Response` - HTTP响应对象
158
+
159
+ ##### `async_send()` - 异步发送短信
160
+
161
+ ```python
162
+ async def async_send(
163
+ client: Optional[httpx.AsyncClient] = None,
164
+ phone_nos: Optional[Union[str, list, tuple]] = None,
165
+ content: Optional[str] = None,
166
+ **kwargs
167
+ ) -> httpx.Response
168
+ ```
169
+
170
+ **参数**:
171
+
172
+ | 参数 | 类型 | 说明 |
173
+ |------|------|------|
174
+ | client | httpx.AsyncClient | 可选的异步HTTP客户端实例 |
175
+ | phone_nos | str/list/tuple | 接收短信的手机号码 |
176
+ | content | str | 短信内容 |
177
+ | **kwargs | - | 传递给httpx.AsyncClient.request的额外参数 |
178
+
179
+ **返回**: `httpx.Response` - HTTP响应对象
180
+
181
+ ## 工具函数
182
+
183
+ ### `timestamp()`
184
+
185
+ 生成当前时间戳(毫秒)
186
+
187
+ ```python
188
+ from py_lmobile.sms.utils import timestamp
189
+
190
+ ts = timestamp() # 返回示例: 1630000000000
191
+ ```
192
+
193
+ ### `random_digits(length: int = 10)`
194
+
195
+ 生成指定长度的随机数字
196
+
197
+ ```python
198
+ from py_lmobile.sms.utils import random_digits
199
+
200
+ digits = random_digits(10) # 返回示例: 1234567890
201
+ ```
202
+
203
+ ### `sha256_signature()`
204
+
205
+ 生成SHA256签名,用于请求认证
206
+
207
+ ```python
208
+ from py_lmobile.sms.utils import sha256_signature
209
+
210
+ signature = sha256_signature(
211
+ account_id="your_account_id",
212
+ password="your_password",
213
+ phone_nos="13800138000",
214
+ random_digits="1234567890",
215
+ timestamp="1630000000000",
216
+ smms_encrypt_key="SMmsEncryptKey"
217
+ )
218
+ ```
219
+
220
+ ### `convert_to_result_eq_succ()`
221
+
222
+ 将HTTP响应转换为标准化的响应模型
223
+
224
+ ```python
225
+ from py_lmobile.sms.utils import convert_to_result_eq_succ
226
+
227
+ response = sms.send(phone_nos="13800138000", content="test")
228
+ result = convert_to_result_eq_succ(response)
229
+ print(result.Result) # "succ"
230
+ ```
231
+
232
+ ### `result_eq_succ_validator()`
233
+
234
+ 校验响应是否符合成功响应的JSON Schema
235
+
236
+ ```python
237
+ from py_lmobile.sms.utils import result_eq_succ_validator
238
+
239
+ response = sms.send(phone_nos="13800138000", content="test")
240
+ is_valid = result_eq_succ_validator(response) # True 或 False
241
+ ```
242
+
243
+ ## 响应模型
244
+
245
+ ### `RESULT_EQ_SUCC`
246
+
247
+ 成功响应模型,继承自Base模型
248
+
249
+ ```python
250
+ from py_lmobile.sms.responses import RESULT_EQ_SUCC
251
+
252
+ result = RESULT_EQ_SUCC(Result="succ")
253
+ print(result.Result) # "succ"
254
+ ```
255
+
256
+ ## 配置说明
257
+
258
+ ### httpx 客户端配置
259
+
260
+ 可以通过 `client_kwargs` 参数自定义 httpx 客户端配置:
261
+
262
+ ```python
263
+ sms = Sms(
264
+ account_id="your_account_id",
265
+ password="your_password",
266
+ product_id="your_product_id",
267
+ client_kwargs={
268
+ "timeout": 30,
269
+ "verify": True,
270
+ "proxies": "http://proxy:8080"
271
+ }
272
+ )
273
+ ```
274
+
275
+ ## 注意事项
276
+
277
+ 1. 密码会在内部进行MD5加密后参与签名计算
278
+ 2. 签名算法使用SHA256,按照API要求的参数顺序拼接
279
+ 3. 多个手机号请使用逗号分隔或传入列表
280
+ 4. 建议复用HTTP客户端以提高性能
281
+
282
+ ## 许可证
283
+
284
+ MIT License
285
+
286
+ ## 主页
287
+ [https://gitee.com/guolei19850528/py_lmobile](https://gitee.com/guolei19850528/py_lmobile)
288
+
289
+ ## 作者
290
+ Guolei<174000902@qq.com>
@@ -0,0 +1,237 @@
1
+ # py-lmobile
2
+
3
+ 微网通联短信服务 Python SDK
4
+
5
+ ## 简介
6
+
7
+ `py_lmobile` 是一个用于接入微网通联(51welink)短信服务的 Python SDK,提供同步和异步两种发送方式,支持请求签名认证。
8
+
9
+ ## 安装
10
+
11
+ ```bash
12
+ pip install py_lmobile
13
+ ```
14
+
15
+ ## 快速开始
16
+
17
+ ### 同步发送短信
18
+
19
+ ```python
20
+ from py_lmobile.sms import Sms
21
+
22
+ # 初始化客户端
23
+ sms = Sms(
24
+ base_url="https://api.51welink.com/",
25
+ account_id="your_account_id",
26
+ password="your_password",
27
+ product_id="your_product_id"
28
+ )
29
+
30
+ # 发送短信
31
+ response = sms.send(
32
+ phone_nos="13800138000",
33
+ content="您的验证码是:123456"
34
+ )
35
+
36
+ # 处理响应
37
+ print(response.json())
38
+ ```
39
+
40
+ ### 异步发送短信
41
+
42
+ ```python
43
+ import asyncio
44
+ from py_lmobile.sms import Sms
45
+
46
+ async def main():
47
+ # 初始化客户端
48
+ sms = Sms(
49
+ base_url="https://api.51welink.com/",
50
+ account_id="your_account_id",
51
+ password="your_password",
52
+ product_id="your_product_id"
53
+ )
54
+
55
+ # 异步发送短信
56
+ response = await sms.async_send(
57
+ phone_nos=["13800138000", "13900139000"],
58
+ content="您的验证码是:123456"
59
+ )
60
+
61
+ # 处理响应
62
+ print(response.json())
63
+
64
+ asyncio.run(main())
65
+ ```
66
+
67
+ ## API 文档
68
+
69
+ ### Sms 类
70
+
71
+ #### 初始化参数
72
+
73
+ | 参数 | 类型 | 必填 | 默认值 | 说明 |
74
+ |------|------|------|--------|------|
75
+ | base_url | str | 否 | `https://api.51welink.com/` | API服务器地址 |
76
+ | account_id | str | 是 | - | 账号ID |
77
+ | password | str | 是 | - | 密码 |
78
+ | product_id | str/int | 是 | - | 产品ID |
79
+ | smms_encrypt_key | str | 否 | `SMmsEncryptKey` | 加密密钥 |
80
+ | client_kwargs | dict | 否 | - | 传递给httpx.Client的额外配置 |
81
+
82
+ #### 方法
83
+
84
+ ##### `send()` - 同步发送短信
85
+
86
+ ```python
87
+ def send(
88
+ client: Optional[httpx.Client] = None,
89
+ phone_nos: Optional[Union[str, list, tuple]] = None,
90
+ content: Optional[str] = None,
91
+ **kwargs
92
+ ) -> httpx.Response
93
+ ```
94
+
95
+ **参数**:
96
+
97
+ | 参数 | 类型 | 说明 |
98
+ |------|------|------|
99
+ | client | httpx.Client | 可选的HTTP客户端实例 |
100
+ | phone_nos | str/list/tuple | 接收短信的手机号码 |
101
+ | content | str | 短信内容 |
102
+ | **kwargs | - | 传递给httpx.Client.request的额外参数 |
103
+
104
+ **返回**: `httpx.Response` - HTTP响应对象
105
+
106
+ ##### `async_send()` - 异步发送短信
107
+
108
+ ```python
109
+ async def async_send(
110
+ client: Optional[httpx.AsyncClient] = None,
111
+ phone_nos: Optional[Union[str, list, tuple]] = None,
112
+ content: Optional[str] = None,
113
+ **kwargs
114
+ ) -> httpx.Response
115
+ ```
116
+
117
+ **参数**:
118
+
119
+ | 参数 | 类型 | 说明 |
120
+ |------|------|------|
121
+ | client | httpx.AsyncClient | 可选的异步HTTP客户端实例 |
122
+ | phone_nos | str/list/tuple | 接收短信的手机号码 |
123
+ | content | str | 短信内容 |
124
+ | **kwargs | - | 传递给httpx.AsyncClient.request的额外参数 |
125
+
126
+ **返回**: `httpx.Response` - HTTP响应对象
127
+
128
+ ## 工具函数
129
+
130
+ ### `timestamp()`
131
+
132
+ 生成当前时间戳(毫秒)
133
+
134
+ ```python
135
+ from py_lmobile.sms.utils import timestamp
136
+
137
+ ts = timestamp() # 返回示例: 1630000000000
138
+ ```
139
+
140
+ ### `random_digits(length: int = 10)`
141
+
142
+ 生成指定长度的随机数字
143
+
144
+ ```python
145
+ from py_lmobile.sms.utils import random_digits
146
+
147
+ digits = random_digits(10) # 返回示例: 1234567890
148
+ ```
149
+
150
+ ### `sha256_signature()`
151
+
152
+ 生成SHA256签名,用于请求认证
153
+
154
+ ```python
155
+ from py_lmobile.sms.utils import sha256_signature
156
+
157
+ signature = sha256_signature(
158
+ account_id="your_account_id",
159
+ password="your_password",
160
+ phone_nos="13800138000",
161
+ random_digits="1234567890",
162
+ timestamp="1630000000000",
163
+ smms_encrypt_key="SMmsEncryptKey"
164
+ )
165
+ ```
166
+
167
+ ### `convert_to_result_eq_succ()`
168
+
169
+ 将HTTP响应转换为标准化的响应模型
170
+
171
+ ```python
172
+ from py_lmobile.sms.utils import convert_to_result_eq_succ
173
+
174
+ response = sms.send(phone_nos="13800138000", content="test")
175
+ result = convert_to_result_eq_succ(response)
176
+ print(result.Result) # "succ"
177
+ ```
178
+
179
+ ### `result_eq_succ_validator()`
180
+
181
+ 校验响应是否符合成功响应的JSON Schema
182
+
183
+ ```python
184
+ from py_lmobile.sms.utils import result_eq_succ_validator
185
+
186
+ response = sms.send(phone_nos="13800138000", content="test")
187
+ is_valid = result_eq_succ_validator(response) # True 或 False
188
+ ```
189
+
190
+ ## 响应模型
191
+
192
+ ### `RESULT_EQ_SUCC`
193
+
194
+ 成功响应模型,继承自Base模型
195
+
196
+ ```python
197
+ from py_lmobile.sms.responses import RESULT_EQ_SUCC
198
+
199
+ result = RESULT_EQ_SUCC(Result="succ")
200
+ print(result.Result) # "succ"
201
+ ```
202
+
203
+ ## 配置说明
204
+
205
+ ### httpx 客户端配置
206
+
207
+ 可以通过 `client_kwargs` 参数自定义 httpx 客户端配置:
208
+
209
+ ```python
210
+ sms = Sms(
211
+ account_id="your_account_id",
212
+ password="your_password",
213
+ product_id="your_product_id",
214
+ client_kwargs={
215
+ "timeout": 30,
216
+ "verify": True,
217
+ "proxies": "http://proxy:8080"
218
+ }
219
+ )
220
+ ```
221
+
222
+ ## 注意事项
223
+
224
+ 1. 密码会在内部进行MD5加密后参与签名计算
225
+ 2. 签名算法使用SHA256,按照API要求的参数顺序拼接
226
+ 3. 多个手机号请使用逗号分隔或传入列表
227
+ 4. 建议复用HTTP客户端以提高性能
228
+
229
+ ## 许可证
230
+
231
+ MIT License
232
+
233
+ ## 主页
234
+ [https://gitee.com/guolei19850528/py_lmobile](https://gitee.com/guolei19850528/py_lmobile)
235
+
236
+ ## 作者
237
+ Guolei<174000902@qq.com>
@@ -0,0 +1,45 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61.0", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+ [project]
5
+ name = "py-lmobile"
6
+ version = "0.1.0"
7
+ requires-python = ">=3.10"
8
+ authors = [
9
+ { name = "Guolei", email = "174000902@qq.com" }
10
+ ]
11
+ description = "一个用于与微网通联 API 交互的 Python 客户端库"
12
+ keywords = ["lmobile", "python", "client", "api", "微网通联","短信"]
13
+ license = { file = "LICENSE" }
14
+ readme = "README.md"
15
+ classifiers = [
16
+ "License :: OSI Approved :: MIT License",
17
+ "Development Status :: 4 - Beta",
18
+ "Intended Audience :: Developers",
19
+ "Programming Language :: Python :: 3",
20
+ "Programming Language :: Python :: 3.10",
21
+ "Programming Language :: Python :: 3.11",
22
+ "Programming Language :: Python :: 3.12",
23
+ "Programming Language :: Python :: 3.13",
24
+ "Operating System :: OS Independent",
25
+ ]
26
+ dependencies = [
27
+ "httpx>=0.27.0",
28
+ "pydantic>=2.0",
29
+ "jsonpath-ng>=1.5.3",
30
+ "jsonschema>=4.21.0",
31
+ ]
32
+ [project.optional-dependencies]
33
+ dev = [
34
+ "pytest>=7.0",
35
+ "pytest-cov>=4.0",
36
+ "setuptools>=61.0",
37
+ "twine>=4.0",
38
+ ]
39
+
40
+ [project.urls]
41
+ "Homepage" = "https://gitee.com/guolei19850528/py_lmobile"
42
+ "Repository" = "https://gitee.com/guolei19850528/py_lmobile.git"
43
+
44
+ [tool.setuptools.packages.find]
45
+ where = ["src"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: UTF-8 -*-
@@ -0,0 +1,177 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: UTF-8 -*-
3
+ from typing import Optional, Union
4
+
5
+ import httpx
6
+ from pydantic import HttpUrl
7
+
8
+ from .utils import timestamp, random_digits, sha256_signature
9
+
10
+
11
+ class Sms:
12
+ def __init__(
13
+ self,
14
+ base_url: Optional[HttpUrl] = "https://api.51welink.com/",
15
+ account_id: Optional[str] = None,
16
+ password: Optional[str] = None,
17
+ product_id: Optional[Union[str, int]] = None,
18
+ smms_encrypt_key: Optional[str] = "SMmsEncryptKey",
19
+ client_kwargs: Optional[dict] = None
20
+ ):
21
+ """
22
+ 初始化短信客户端
23
+ Args:
24
+ base_url: API 服务器地址,如 "https://api.51welink.com/"
25
+ account_id:
26
+ password:
27
+ product_id:
28
+ smms_encrypt_key:
29
+ client_kwargs: 传递给 httpx.Client 的额外配置参数
30
+ """
31
+ self.base_url = base_url or ""
32
+ self.base_url = self.base_url[:-1] if self.base_url.endswith("/") else self.base_url
33
+ self.account_id = account_id or ""
34
+ self.password = password or ""
35
+ self.product_id = product_id or ""
36
+ self.smms_encrypt_key = smms_encrypt_key or ""
37
+ self.client_kwargs = client_kwargs or {}
38
+ self.client_kwargs = {
39
+ **{
40
+ "base_url": self.base_url,
41
+ "verify": False,
42
+ "timeout": 60,
43
+ },
44
+ **self.client_kwargs,
45
+ }
46
+
47
+ def client(self) -> httpx.Client:
48
+ """
49
+ 创建并返回同步HTTP客户端
50
+
51
+ Returns:
52
+ httpx.Client: 配置好的同步HTTP客户端实例
53
+ """
54
+ return httpx.Client(**self.client_kwargs)
55
+
56
+ def async_client(self) -> httpx.AsyncClient:
57
+ """
58
+ 创建并返回异步HTTP客户端
59
+
60
+ Returns:
61
+ httpx.AsyncClient: 配置好的异步HTTP客户端实例
62
+ """
63
+ return httpx.AsyncClient(**self.client_kwargs)
64
+
65
+ def send(
66
+ self,
67
+ client: Optional[httpx.Client] = None,
68
+ phone_nos: Optional[Union[str, list, tuple]] = None,
69
+ content: Optional[str] = None,
70
+ **kwargs
71
+ ) -> httpx.Response:
72
+ """
73
+ 同步发送短信
74
+
75
+ Args:
76
+ client: 可选的HTTP客户端实例,若不传则自动创建
77
+ phone_nos: 接收短信的手机号码,支持单个字符串或列表/元组
78
+ content: 短信内容
79
+ **kwargs: 传递给 httpx.Client.request 的额外参数
80
+
81
+ Returns:
82
+ httpx.Response: HTTP响应对象
83
+
84
+ Raises:
85
+ Exception: 发送过程中可能抛出的异常
86
+ """
87
+ if isinstance(phone_nos, (list, tuple)):
88
+ phone_nos = ",".join(phone_nos)
89
+ ts = timestamp()
90
+ digits = random_digits()
91
+ kwargs = kwargs or {}
92
+ kwargs = {
93
+ **{
94
+ "method": "POST",
95
+ "url": "/EncryptionSubmit/SendSms.ashx",
96
+ "data": {
97
+ "AccountID": self.account_id,
98
+ "ProductID": self.product_id,
99
+ "PhoneNos": phone_nos,
100
+ "Timestamp": ts,
101
+ "Random": digits,
102
+ "Content": content,
103
+ "AccessKey": sha256_signature(
104
+ account_id=self.account_id,
105
+ password=self.password,
106
+ phone_nos=phone_nos,
107
+ random_digits=digits,
108
+ timestamp=ts,
109
+ smms_encrypt_key=self.smms_encrypt_key,
110
+ )
111
+ },
112
+ },
113
+ **kwargs
114
+ }
115
+ if isinstance(client, httpx.Client):
116
+ response = client.request(**kwargs)
117
+ else:
118
+ with self.client() as _client:
119
+ response = _client.request(**kwargs)
120
+ return response
121
+
122
+ async def async_send(
123
+ self,
124
+ client: Optional[httpx.AsyncClient] = None,
125
+ phone_nos: Optional[Union[str, list, tuple]] = None,
126
+ content: Optional[str] = None,
127
+ **kwargs
128
+ ) -> httpx.Response:
129
+ """
130
+ 异步发送短信
131
+
132
+ Args:
133
+ client: 可选的异步HTTP客户端实例,若不传则自动创建
134
+ phone_nos: 接收短信的手机号码,支持单个字符串或列表/元组
135
+ content: 短信内容
136
+ **kwargs: 传递给 httpx.AsyncClient.request 的额外参数
137
+
138
+ Returns:
139
+ httpx.Response: HTTP响应对象
140
+
141
+ Raises:
142
+ Exception: 发送过程中可能抛出的异常
143
+ """
144
+ if isinstance(phone_nos, (list, tuple)):
145
+ phone_nos = ",".join(phone_nos)
146
+ ts = timestamp()
147
+ digits = random_digits()
148
+ kwargs = kwargs or {}
149
+ kwargs = {
150
+ **{
151
+ "method": "POST",
152
+ "url": "/EncryptionSubmit/SendSms.ashx",
153
+ "data": {
154
+ "AccountID": self.account_id,
155
+ "ProductID": self.product_id,
156
+ "PhoneNos": phone_nos,
157
+ "Timestamp": ts,
158
+ "Random": digits,
159
+ "Content": content,
160
+ "AccessKey": sha256_signature(
161
+ account_id=self.account_id,
162
+ password=self.password,
163
+ phone_nos=phone_nos,
164
+ random_digits=digits,
165
+ timestamp=ts,
166
+ smms_encrypt_key=self.smms_encrypt_key,
167
+ )
168
+ },
169
+ },
170
+ **kwargs
171
+ }
172
+ if isinstance(client, httpx.AsyncClient):
173
+ response = await client.request(**kwargs)
174
+ else:
175
+ async with self.async_client() as _client:
176
+ response = await _client.request(**kwargs)
177
+ return response
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: UTF-8 -*-
3
+ from pydantic import BaseModel
4
+ from pydantic import Field
5
+ from typing import Literal, Union, Any, Optional
6
+
7
+
8
+ class Base(BaseModel):
9
+ """
10
+ lmobile响应基类
11
+
12
+ 所有API响应都包含错误码和错误信息字段
13
+ """
14
+ Result: str = Field(..., title="错误码", description="错误码,succ表示成功,其他表示失败")
15
+
16
+ model_config = {
17
+ "extra": "allow" # 允许额外动态字段
18
+ }
19
+
20
+
21
+ class RESULT_EQ_SUCC(Base):
22
+ """
23
+ 成功响应模型
24
+
25
+ 继承自Base,用于成功接口的响应
26
+ """
27
+ Result: Literal["succ"] = Field(..., title="错误码", description="成功,值为succ")
@@ -0,0 +1,151 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: UTF-8 -*-
3
+ import hashlib
4
+ import random
5
+ import string
6
+ from datetime import datetime
7
+ from typing import Any, Optional, Union
8
+
9
+ import httpx
10
+ from jsonpath_ng import parse
11
+ from jsonschema.validators import Draft202012Validator
12
+
13
+ from .responses import RESULT_EQ_SUCC
14
+
15
+
16
+ def json_find_first(expression: str, data: Any) -> Any:
17
+ """
18
+ 使用 JSONPath 表达式从数据中查找第一个匹配项
19
+
20
+ Args:
21
+ expression: JSONPath 表达式,用于指定要查找的数据路径
22
+ data: 待查询的 JSON 数据
23
+
24
+ Returns:
25
+ Any: 第一个匹配的值,如果没有匹配项则返回 None
26
+ """
27
+ results = [i.value for i in parse(expression).find(data)]
28
+ if isinstance(results, list) and len(results) > 0:
29
+ return results[0]
30
+ return None
31
+
32
+
33
+ def json_is_valid(schema: Optional[dict], data: Any) -> bool:
34
+ """
35
+ 校验 JSON 数据是否符合指定的 JSON Schema
36
+
37
+ Args:
38
+ schema: JSON Schema 字典
39
+ data: 待校验的 JSON 数据
40
+
41
+ Returns:
42
+ bool: 校验是否成功
43
+ """
44
+ return Draft202012Validator(schema).is_valid(data)
45
+
46
+
47
+ def timestamp() -> int:
48
+ """
49
+ 生成当前时间戳(毫秒)
50
+
51
+ 返回:
52
+ int: 当前时间戳(毫秒),用于请求的时效性验证
53
+
54
+ 示例:
55
+ >>> ts = utils.timestamp()
56
+ >>> print(ts) # 输出示例: 1630000000000
57
+ """
58
+ return int((datetime.now().timestamp() * 1000))
59
+
60
+
61
+ def random_digits(length: int = 10) -> int:
62
+ return int("".join(random.sample(string.digits, length)))
63
+
64
+
65
+ def sha256_signature(
66
+ account_id: str = "",
67
+ password: str = "",
68
+ phone_nos: str = "",
69
+ random_digits: Optional[Union[int, str]] = "",
70
+ timestamp: Optional[Union[int, str]] = "",
71
+ smms_encrypt_key: str = ""
72
+ ) -> str:
73
+ """
74
+ 生成SHA256签名
75
+
76
+ 按照微网通联API要求的格式生成签名,用于短信发送请求的认证
77
+
78
+ Args:
79
+ account_id: 账号ID
80
+ password: 密码
81
+ phone_nos: 手机号,多个手机号用逗号分隔
82
+ random_digits: 随机数字
83
+ timestamp: 时间戳
84
+ smms_encrypt_key: 加密密钥
85
+
86
+ Returns:
87
+ str: SHA256签名结果
88
+ """
89
+ # 构建签名字符串 - 按照API要求的顺序和格式拼接参数
90
+ # 1. 先对密码进行MD5加密,使用加密密钥作为盐值
91
+ password_md5 = hashlib.md5(f"{password}{smms_encrypt_key}".encode('utf-8')).hexdigest()
92
+
93
+ # 2. 按照API要求的格式拼接参数
94
+ temp_string = "&".join([
95
+ f"AccountId={account_id}",
96
+ f"PhoneNos={phone_nos.split(',')[0]}", # 只取第一个手机号用于签名
97
+ f"Password={password_md5.upper()}", # 密码使用MD5加密后转大写
98
+ f"Random={random_digits}",
99
+ f"Timestamp={timestamp}",
100
+ ])
101
+
102
+ # 3. 对拼接后的字符串进行SHA256加密
103
+ return hashlib.sha256(temp_string.encode("utf-8")).hexdigest()
104
+
105
+
106
+
107
+ def convert_to_result_eq_succ(response: Union[httpx.Response, dict]) -> RESULT_EQ_SUCC:
108
+ """
109
+ 将 HTTP 响应或字典转换为 RESULT_EQ_SUCC 模型对象
110
+
111
+ 统一处理 httpx.Response 对象和字典,将其转换为标准化的 RESULT_EQ_SUCC 响应模型。
112
+
113
+ 参数:
114
+ response (Union[httpx.Response, dict]): HTTP 响应对象或字典数据
115
+
116
+ 返回:
117
+ RESULT_EQ_SUCC: 标准化的响应模型对象,包含 Result 字段
118
+
119
+ 处理逻辑:
120
+ - 如果输入是 httpx.Response 对象,先调用 json() 方法解析响应体
121
+ - 如果输入是字典,直接传入 RESULT_EQ_SUCC 构造函数
122
+ """
123
+ if isinstance(response, httpx.Response):
124
+ return RESULT_EQ_SUCC(**response.json())
125
+ return RESULT_EQ_SUCC(**response)
126
+
127
+
128
+ def result_eq_succ_validator(
129
+ response: Union[httpx.Response, dict] = None,
130
+ schema: dict = {
131
+ "type": "object",
132
+ "properties": {
133
+ "Result": {
134
+ "oneOf": [
135
+ {"type": "string", "const": "succ"}
136
+ ]
137
+ },
138
+ },
139
+ "required": ["Result"]
140
+ }
141
+ ):
142
+ """
143
+ 校验 HTTP 响应或字典是否符合指定的 JSON Schema
144
+
145
+ :param response: HTTP 响应对象或字典数据
146
+ :param schema: JSON Schema 字典
147
+ :return: 校验是否成功
148
+ """
149
+ if isinstance(response, httpx.Response):
150
+ return json_is_valid(schema, response.json())
151
+ return json_is_valid(schema, response)
@@ -0,0 +1,290 @@
1
+ Metadata-Version: 2.4
2
+ Name: py-lmobile
3
+ Version: 0.1.0
4
+ Summary: 一个用于与微网通联 API 交互的 Python 客户端库
5
+ Author-email: Guolei <174000902@qq.com>
6
+ License: MIT License
7
+
8
+ Copyright (c) 2026 郭磊
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+
28
+ Project-URL: Homepage, https://gitee.com/guolei19850528/py_lmobile
29
+ Project-URL: Repository, https://gitee.com/guolei19850528/py_lmobile.git
30
+ Keywords: lmobile,python,client,api,微网通联,短信
31
+ Classifier: License :: OSI Approved :: MIT License
32
+ Classifier: Development Status :: 4 - Beta
33
+ Classifier: Intended Audience :: Developers
34
+ Classifier: Programming Language :: Python :: 3
35
+ Classifier: Programming Language :: Python :: 3.10
36
+ Classifier: Programming Language :: Python :: 3.11
37
+ Classifier: Programming Language :: Python :: 3.12
38
+ Classifier: Programming Language :: Python :: 3.13
39
+ Classifier: Operating System :: OS Independent
40
+ Requires-Python: >=3.10
41
+ Description-Content-Type: text/markdown
42
+ License-File: LICENSE
43
+ Requires-Dist: httpx>=0.27.0
44
+ Requires-Dist: pydantic>=2.0
45
+ Requires-Dist: jsonpath-ng>=1.5.3
46
+ Requires-Dist: jsonschema>=4.21.0
47
+ Provides-Extra: dev
48
+ Requires-Dist: pytest>=7.0; extra == "dev"
49
+ Requires-Dist: pytest-cov>=4.0; extra == "dev"
50
+ Requires-Dist: setuptools>=61.0; extra == "dev"
51
+ Requires-Dist: twine>=4.0; extra == "dev"
52
+ Dynamic: license-file
53
+
54
+ # py-lmobile
55
+
56
+ 微网通联短信服务 Python SDK
57
+
58
+ ## 简介
59
+
60
+ `py_lmobile` 是一个用于接入微网通联(51welink)短信服务的 Python SDK,提供同步和异步两种发送方式,支持请求签名认证。
61
+
62
+ ## 安装
63
+
64
+ ```bash
65
+ pip install py_lmobile
66
+ ```
67
+
68
+ ## 快速开始
69
+
70
+ ### 同步发送短信
71
+
72
+ ```python
73
+ from py_lmobile.sms import Sms
74
+
75
+ # 初始化客户端
76
+ sms = Sms(
77
+ base_url="https://api.51welink.com/",
78
+ account_id="your_account_id",
79
+ password="your_password",
80
+ product_id="your_product_id"
81
+ )
82
+
83
+ # 发送短信
84
+ response = sms.send(
85
+ phone_nos="13800138000",
86
+ content="您的验证码是:123456"
87
+ )
88
+
89
+ # 处理响应
90
+ print(response.json())
91
+ ```
92
+
93
+ ### 异步发送短信
94
+
95
+ ```python
96
+ import asyncio
97
+ from py_lmobile.sms import Sms
98
+
99
+ async def main():
100
+ # 初始化客户端
101
+ sms = Sms(
102
+ base_url="https://api.51welink.com/",
103
+ account_id="your_account_id",
104
+ password="your_password",
105
+ product_id="your_product_id"
106
+ )
107
+
108
+ # 异步发送短信
109
+ response = await sms.async_send(
110
+ phone_nos=["13800138000", "13900139000"],
111
+ content="您的验证码是:123456"
112
+ )
113
+
114
+ # 处理响应
115
+ print(response.json())
116
+
117
+ asyncio.run(main())
118
+ ```
119
+
120
+ ## API 文档
121
+
122
+ ### Sms 类
123
+
124
+ #### 初始化参数
125
+
126
+ | 参数 | 类型 | 必填 | 默认值 | 说明 |
127
+ |------|------|------|--------|------|
128
+ | base_url | str | 否 | `https://api.51welink.com/` | API服务器地址 |
129
+ | account_id | str | 是 | - | 账号ID |
130
+ | password | str | 是 | - | 密码 |
131
+ | product_id | str/int | 是 | - | 产品ID |
132
+ | smms_encrypt_key | str | 否 | `SMmsEncryptKey` | 加密密钥 |
133
+ | client_kwargs | dict | 否 | - | 传递给httpx.Client的额外配置 |
134
+
135
+ #### 方法
136
+
137
+ ##### `send()` - 同步发送短信
138
+
139
+ ```python
140
+ def send(
141
+ client: Optional[httpx.Client] = None,
142
+ phone_nos: Optional[Union[str, list, tuple]] = None,
143
+ content: Optional[str] = None,
144
+ **kwargs
145
+ ) -> httpx.Response
146
+ ```
147
+
148
+ **参数**:
149
+
150
+ | 参数 | 类型 | 说明 |
151
+ |------|------|------|
152
+ | client | httpx.Client | 可选的HTTP客户端实例 |
153
+ | phone_nos | str/list/tuple | 接收短信的手机号码 |
154
+ | content | str | 短信内容 |
155
+ | **kwargs | - | 传递给httpx.Client.request的额外参数 |
156
+
157
+ **返回**: `httpx.Response` - HTTP响应对象
158
+
159
+ ##### `async_send()` - 异步发送短信
160
+
161
+ ```python
162
+ async def async_send(
163
+ client: Optional[httpx.AsyncClient] = None,
164
+ phone_nos: Optional[Union[str, list, tuple]] = None,
165
+ content: Optional[str] = None,
166
+ **kwargs
167
+ ) -> httpx.Response
168
+ ```
169
+
170
+ **参数**:
171
+
172
+ | 参数 | 类型 | 说明 |
173
+ |------|------|------|
174
+ | client | httpx.AsyncClient | 可选的异步HTTP客户端实例 |
175
+ | phone_nos | str/list/tuple | 接收短信的手机号码 |
176
+ | content | str | 短信内容 |
177
+ | **kwargs | - | 传递给httpx.AsyncClient.request的额外参数 |
178
+
179
+ **返回**: `httpx.Response` - HTTP响应对象
180
+
181
+ ## 工具函数
182
+
183
+ ### `timestamp()`
184
+
185
+ 生成当前时间戳(毫秒)
186
+
187
+ ```python
188
+ from py_lmobile.sms.utils import timestamp
189
+
190
+ ts = timestamp() # 返回示例: 1630000000000
191
+ ```
192
+
193
+ ### `random_digits(length: int = 10)`
194
+
195
+ 生成指定长度的随机数字
196
+
197
+ ```python
198
+ from py_lmobile.sms.utils import random_digits
199
+
200
+ digits = random_digits(10) # 返回示例: 1234567890
201
+ ```
202
+
203
+ ### `sha256_signature()`
204
+
205
+ 生成SHA256签名,用于请求认证
206
+
207
+ ```python
208
+ from py_lmobile.sms.utils import sha256_signature
209
+
210
+ signature = sha256_signature(
211
+ account_id="your_account_id",
212
+ password="your_password",
213
+ phone_nos="13800138000",
214
+ random_digits="1234567890",
215
+ timestamp="1630000000000",
216
+ smms_encrypt_key="SMmsEncryptKey"
217
+ )
218
+ ```
219
+
220
+ ### `convert_to_result_eq_succ()`
221
+
222
+ 将HTTP响应转换为标准化的响应模型
223
+
224
+ ```python
225
+ from py_lmobile.sms.utils import convert_to_result_eq_succ
226
+
227
+ response = sms.send(phone_nos="13800138000", content="test")
228
+ result = convert_to_result_eq_succ(response)
229
+ print(result.Result) # "succ"
230
+ ```
231
+
232
+ ### `result_eq_succ_validator()`
233
+
234
+ 校验响应是否符合成功响应的JSON Schema
235
+
236
+ ```python
237
+ from py_lmobile.sms.utils import result_eq_succ_validator
238
+
239
+ response = sms.send(phone_nos="13800138000", content="test")
240
+ is_valid = result_eq_succ_validator(response) # True 或 False
241
+ ```
242
+
243
+ ## 响应模型
244
+
245
+ ### `RESULT_EQ_SUCC`
246
+
247
+ 成功响应模型,继承自Base模型
248
+
249
+ ```python
250
+ from py_lmobile.sms.responses import RESULT_EQ_SUCC
251
+
252
+ result = RESULT_EQ_SUCC(Result="succ")
253
+ print(result.Result) # "succ"
254
+ ```
255
+
256
+ ## 配置说明
257
+
258
+ ### httpx 客户端配置
259
+
260
+ 可以通过 `client_kwargs` 参数自定义 httpx 客户端配置:
261
+
262
+ ```python
263
+ sms = Sms(
264
+ account_id="your_account_id",
265
+ password="your_password",
266
+ product_id="your_product_id",
267
+ client_kwargs={
268
+ "timeout": 30,
269
+ "verify": True,
270
+ "proxies": "http://proxy:8080"
271
+ }
272
+ )
273
+ ```
274
+
275
+ ## 注意事项
276
+
277
+ 1. 密码会在内部进行MD5加密后参与签名计算
278
+ 2. 签名算法使用SHA256,按照API要求的参数顺序拼接
279
+ 3. 多个手机号请使用逗号分隔或传入列表
280
+ 4. 建议复用HTTP客户端以提高性能
281
+
282
+ ## 许可证
283
+
284
+ MIT License
285
+
286
+ ## 主页
287
+ [https://gitee.com/guolei19850528/py_lmobile](https://gitee.com/guolei19850528/py_lmobile)
288
+
289
+ ## 作者
290
+ Guolei<174000902@qq.com>
@@ -0,0 +1,12 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ src/py_lmobile/__init__.py
5
+ src/py_lmobile.egg-info/PKG-INFO
6
+ src/py_lmobile.egg-info/SOURCES.txt
7
+ src/py_lmobile.egg-info/dependency_links.txt
8
+ src/py_lmobile.egg-info/requires.txt
9
+ src/py_lmobile.egg-info/top_level.txt
10
+ src/py_lmobile/sms/__init__.py
11
+ src/py_lmobile/sms/responses.py
12
+ src/py_lmobile/sms/utils.py
@@ -0,0 +1,10 @@
1
+ httpx>=0.27.0
2
+ pydantic>=2.0
3
+ jsonpath-ng>=1.5.3
4
+ jsonschema>=4.21.0
5
+
6
+ [dev]
7
+ pytest>=7.0
8
+ pytest-cov>=4.0
9
+ setuptools>=61.0
10
+ twine>=4.0
@@ -0,0 +1 @@
1
+ py_lmobile