zmqtt 0.0.1__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.
- zmqtt-0.0.1/LICENSE +21 -0
- zmqtt-0.0.1/PKG-INFO +144 -0
- zmqtt-0.0.1/README.md +116 -0
- zmqtt-0.0.1/pyproject.toml +98 -0
- zmqtt-0.0.1/src/zmqtt/__init__.py +39 -0
- zmqtt-0.0.1/src/zmqtt/_internal/__init__.py +0 -0
- zmqtt-0.0.1/src/zmqtt/_internal/_compat.py +30 -0
- zmqtt-0.0.1/src/zmqtt/_internal/packets/__init__.py +62 -0
- zmqtt-0.0.1/src/zmqtt/_internal/packets/_wire.py +78 -0
- zmqtt-0.0.1/src/zmqtt/_internal/packets/auth.py +15 -0
- zmqtt-0.0.1/src/zmqtt/_internal/packets/codec.py +634 -0
- zmqtt-0.0.1/src/zmqtt/_internal/packets/connect.py +43 -0
- zmqtt-0.0.1/src/zmqtt/_internal/packets/disconnect.py +13 -0
- zmqtt-0.0.1/src/zmqtt/_internal/packets/ping.py +14 -0
- zmqtt-0.0.1/src/zmqtt/_internal/packets/properties.py +720 -0
- zmqtt-0.0.1/src/zmqtt/_internal/packets/publish.py +55 -0
- zmqtt-0.0.1/src/zmqtt/_internal/packets/reader.py +31 -0
- zmqtt-0.0.1/src/zmqtt/_internal/packets/subscribe.py +62 -0
- zmqtt-0.0.1/src/zmqtt/_internal/packets/types.py +34 -0
- zmqtt-0.0.1/src/zmqtt/_internal/protocol.py +544 -0
- zmqtt-0.0.1/src/zmqtt/_internal/state.py +98 -0
- zmqtt-0.0.1/src/zmqtt/_internal/transport/__init__.py +5 -0
- zmqtt-0.0.1/src/zmqtt/_internal/transport/base.py +53 -0
- zmqtt-0.0.1/src/zmqtt/_internal/transport/tcp.py +8 -0
- zmqtt-0.0.1/src/zmqtt/_internal/transport/tls.py +14 -0
- zmqtt-0.0.1/src/zmqtt/_internal/types/__init__.py +0 -0
- zmqtt-0.0.1/src/zmqtt/_internal/types/message.py +42 -0
- zmqtt-0.0.1/src/zmqtt/_internal/types/qos.py +16 -0
- zmqtt-0.0.1/src/zmqtt/_internal/types/retain_handling.py +11 -0
- zmqtt-0.0.1/src/zmqtt/_internal/types/topic.py +64 -0
- zmqtt-0.0.1/src/zmqtt/client.py +665 -0
- zmqtt-0.0.1/src/zmqtt/errors.py +22 -0
- zmqtt-0.0.1/src/zmqtt/py.typed +0 -0
zmqtt-0.0.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Boris Alekseev
|
|
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.
|
zmqtt-0.0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: zmqtt
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Pure asyncio MQTT 3.1.1/5.0 client library
|
|
5
|
+
Keywords: mqtt,mqtt5,iot,python-mqtt,zmqtt
|
|
6
|
+
Author: borisalekseev
|
|
7
|
+
Author-email: borisalekseev <i.borisalekseev@gmail.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
19
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
20
|
+
Classifier: Framework :: AsyncIO
|
|
21
|
+
Classifier: Typing :: Typed
|
|
22
|
+
Requires-Python: >=3.10
|
|
23
|
+
Project-URL: Homepage, https://github.com/faststream-community/zMQTT
|
|
24
|
+
Project-URL: Repository, https://github.com/faststream-community/zMQTT
|
|
25
|
+
Project-URL: Documentation, https://faststream-community.github.io/zMQTT/
|
|
26
|
+
Project-URL: Issues, https://github.com/faststream-community/zMQTT/issues
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
|
|
29
|
+
# zmqtt
|
|
30
|
+
|
|
31
|
+
Pure asyncio MQTT 3.1.1 and 5.0 client library. No paho dependency, no threading, no god classes. See [documentation](https://faststream-community.github.io/zMQTT/).
|
|
32
|
+
|
|
33
|
+
## Why not aiomqtt?
|
|
34
|
+
|
|
35
|
+
[aiomqtt](https://github.com/sbtinstruments/aiomqtt) is a thin async wrapper around paho-mqtt.
|
|
36
|
+
You inherit paho's threading model, 10 000-line files, and implicit global state — just with `async/await` painted on top.
|
|
37
|
+
|
|
38
|
+
zmqtt is built from scratch:
|
|
39
|
+
|
|
40
|
+
| | zmqtt | aiomqtt (paho) |
|
|
41
|
+
| ---------------- | ------------------------------------ | ----------------------------- |
|
|
42
|
+
| I/O model | pure asyncio | paho threads + asyncio bridge |
|
|
43
|
+
| Packet codec | pure functions, I/O-free | paho internals |
|
|
44
|
+
| MQTT 5.0 | native, typed properties dataclasses | partial |
|
|
45
|
+
| Type annotations | strict mypy | partial |
|
|
46
|
+
| Backpressure | bounded subscription queues | none |
|
|
47
|
+
|
|
48
|
+
## Installation
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
pip install zmqtt
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Quick start
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
import asyncio
|
|
58
|
+
from zmqtt import MQTTClient
|
|
59
|
+
|
|
60
|
+
async def main():
|
|
61
|
+
async with MQTTClient("broker.example.com") as client:
|
|
62
|
+
async with client.subscribe("sensors/#") as messages:
|
|
63
|
+
async for msg in messages:
|
|
64
|
+
print(msg.topic, msg.payload)
|
|
65
|
+
|
|
66
|
+
asyncio.run(main())
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Or manage connections and subscriptions manually:
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
import asyncio
|
|
73
|
+
from zmqtt import MQTTClient
|
|
74
|
+
|
|
75
|
+
async def main():
|
|
76
|
+
client = MQTTClient("broker.example.com")
|
|
77
|
+
await client.connect()
|
|
78
|
+
|
|
79
|
+
subscription = client.subscribe("sensors/#")
|
|
80
|
+
await subscription.start()
|
|
81
|
+
|
|
82
|
+
msg = await subscription.get_message()
|
|
83
|
+
print(msg.topic, msg.payload)
|
|
84
|
+
|
|
85
|
+
await subscription.stop()
|
|
86
|
+
await client.disconnect()
|
|
87
|
+
|
|
88
|
+
asyncio.run(main())
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Publish
|
|
92
|
+
|
|
93
|
+
```python
|
|
94
|
+
async with MQTTClient("broker.example.com") as client:
|
|
95
|
+
await client.publish("sensors/temperature", b"23.5", qos=1)
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## QoS levels
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
from zmqtt import QoS
|
|
102
|
+
|
|
103
|
+
await client.publish("topic", b"data", qos=QoS.AT_LEAST_ONCE) # QoS 1
|
|
104
|
+
await client.publish("topic", b"data", qos=QoS.EXACTLY_ONCE) # QoS 2
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## Manual acknowledgement
|
|
108
|
+
|
|
109
|
+
Hold the PUBACK/PUBREC until your application has durably processed the message:
|
|
110
|
+
|
|
111
|
+
```python
|
|
112
|
+
async with client.subscribe("orders/#", auto_ack=False) as messages:
|
|
113
|
+
async for msg in messages:
|
|
114
|
+
await save_to_database(msg)
|
|
115
|
+
await msg.ack() # broker will redeliver if we crash before this
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## Subscription as explicit get
|
|
119
|
+
|
|
120
|
+
Useful when interleaving message handling with other async work:
|
|
121
|
+
|
|
122
|
+
```python
|
|
123
|
+
async with client.subscribe("sensors/#") as messages:
|
|
124
|
+
msg = await messages.get_message()
|
|
125
|
+
print(msg.topic, msg.payload)
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## Reconnection
|
|
129
|
+
|
|
130
|
+
`MQTTClient` reconnects automatically with exponential backoff. Active subscriptions
|
|
131
|
+
are transparently re-registered after reconnect — your `async for` loop keeps running.
|
|
132
|
+
|
|
133
|
+
## MQTT 5.0
|
|
134
|
+
|
|
135
|
+
Pass `version=5` to use MQTT 5.0. Properties are typed dataclasses:
|
|
136
|
+
|
|
137
|
+
```python
|
|
138
|
+
from zmqtt import MQTTClient
|
|
139
|
+
from zmqtt._internal.packets.properties import PublishProperties
|
|
140
|
+
|
|
141
|
+
async with MQTTClient("broker.example.com", version=5) as client:
|
|
142
|
+
props = PublishProperties(content_type="application/json")
|
|
143
|
+
await client.publish("topic", b'{"value": 42}', properties=props)
|
|
144
|
+
```
|
zmqtt-0.0.1/README.md
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# zmqtt
|
|
2
|
+
|
|
3
|
+
Pure asyncio MQTT 3.1.1 and 5.0 client library. No paho dependency, no threading, no god classes. See [documentation](https://faststream-community.github.io/zMQTT/).
|
|
4
|
+
|
|
5
|
+
## Why not aiomqtt?
|
|
6
|
+
|
|
7
|
+
[aiomqtt](https://github.com/sbtinstruments/aiomqtt) is a thin async wrapper around paho-mqtt.
|
|
8
|
+
You inherit paho's threading model, 10 000-line files, and implicit global state — just with `async/await` painted on top.
|
|
9
|
+
|
|
10
|
+
zmqtt is built from scratch:
|
|
11
|
+
|
|
12
|
+
| | zmqtt | aiomqtt (paho) |
|
|
13
|
+
| ---------------- | ------------------------------------ | ----------------------------- |
|
|
14
|
+
| I/O model | pure asyncio | paho threads + asyncio bridge |
|
|
15
|
+
| Packet codec | pure functions, I/O-free | paho internals |
|
|
16
|
+
| MQTT 5.0 | native, typed properties dataclasses | partial |
|
|
17
|
+
| Type annotations | strict mypy | partial |
|
|
18
|
+
| Backpressure | bounded subscription queues | none |
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pip install zmqtt
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Quick start
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
import asyncio
|
|
30
|
+
from zmqtt import MQTTClient
|
|
31
|
+
|
|
32
|
+
async def main():
|
|
33
|
+
async with MQTTClient("broker.example.com") as client:
|
|
34
|
+
async with client.subscribe("sensors/#") as messages:
|
|
35
|
+
async for msg in messages:
|
|
36
|
+
print(msg.topic, msg.payload)
|
|
37
|
+
|
|
38
|
+
asyncio.run(main())
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Or manage connections and subscriptions manually:
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
import asyncio
|
|
45
|
+
from zmqtt import MQTTClient
|
|
46
|
+
|
|
47
|
+
async def main():
|
|
48
|
+
client = MQTTClient("broker.example.com")
|
|
49
|
+
await client.connect()
|
|
50
|
+
|
|
51
|
+
subscription = client.subscribe("sensors/#")
|
|
52
|
+
await subscription.start()
|
|
53
|
+
|
|
54
|
+
msg = await subscription.get_message()
|
|
55
|
+
print(msg.topic, msg.payload)
|
|
56
|
+
|
|
57
|
+
await subscription.stop()
|
|
58
|
+
await client.disconnect()
|
|
59
|
+
|
|
60
|
+
asyncio.run(main())
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Publish
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
async with MQTTClient("broker.example.com") as client:
|
|
67
|
+
await client.publish("sensors/temperature", b"23.5", qos=1)
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## QoS levels
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
from zmqtt import QoS
|
|
74
|
+
|
|
75
|
+
await client.publish("topic", b"data", qos=QoS.AT_LEAST_ONCE) # QoS 1
|
|
76
|
+
await client.publish("topic", b"data", qos=QoS.EXACTLY_ONCE) # QoS 2
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Manual acknowledgement
|
|
80
|
+
|
|
81
|
+
Hold the PUBACK/PUBREC until your application has durably processed the message:
|
|
82
|
+
|
|
83
|
+
```python
|
|
84
|
+
async with client.subscribe("orders/#", auto_ack=False) as messages:
|
|
85
|
+
async for msg in messages:
|
|
86
|
+
await save_to_database(msg)
|
|
87
|
+
await msg.ack() # broker will redeliver if we crash before this
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Subscription as explicit get
|
|
91
|
+
|
|
92
|
+
Useful when interleaving message handling with other async work:
|
|
93
|
+
|
|
94
|
+
```python
|
|
95
|
+
async with client.subscribe("sensors/#") as messages:
|
|
96
|
+
msg = await messages.get_message()
|
|
97
|
+
print(msg.topic, msg.payload)
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Reconnection
|
|
101
|
+
|
|
102
|
+
`MQTTClient` reconnects automatically with exponential backoff. Active subscriptions
|
|
103
|
+
are transparently re-registered after reconnect — your `async for` loop keeps running.
|
|
104
|
+
|
|
105
|
+
## MQTT 5.0
|
|
106
|
+
|
|
107
|
+
Pass `version=5` to use MQTT 5.0. Properties are typed dataclasses:
|
|
108
|
+
|
|
109
|
+
```python
|
|
110
|
+
from zmqtt import MQTTClient
|
|
111
|
+
from zmqtt._internal.packets.properties import PublishProperties
|
|
112
|
+
|
|
113
|
+
async with MQTTClient("broker.example.com", version=5) as client:
|
|
114
|
+
props = PublishProperties(content_type="application/json")
|
|
115
|
+
await client.publish("topic", b'{"value": 42}', properties=props)
|
|
116
|
+
```
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "zmqtt"
|
|
3
|
+
version = "0.0.1"
|
|
4
|
+
description = "Pure asyncio MQTT 3.1.1/5.0 client library"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
authors = [
|
|
9
|
+
{ name = "borisalekseev", email = "i.borisalekseev@gmail.com" }
|
|
10
|
+
]
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
|
+
dependencies = []
|
|
13
|
+
keywords = ["mqtt", "mqtt5", "iot", "python-mqtt", "zmqtt"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 3 - Alpha",
|
|
16
|
+
"Intended Audience :: Developers",
|
|
17
|
+
"License :: OSI Approved :: MIT License",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Programming Language :: Python :: 3.10",
|
|
20
|
+
"Programming Language :: Python :: 3.11",
|
|
21
|
+
"Programming Language :: Python :: 3.12",
|
|
22
|
+
"Programming Language :: Python :: 3.13",
|
|
23
|
+
"Programming Language :: Python :: 3.14",
|
|
24
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
25
|
+
"Framework :: AsyncIO",
|
|
26
|
+
"Typing :: Typed",
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
[project.urls]
|
|
30
|
+
Homepage = "https://github.com/faststream-community/zMQTT"
|
|
31
|
+
Repository = "https://github.com/faststream-community/zMQTT"
|
|
32
|
+
Documentation = "https://faststream-community.github.io/zMQTT/"
|
|
33
|
+
Issues = "https://github.com/faststream-community/zMQTT/issues"
|
|
34
|
+
|
|
35
|
+
[build-system]
|
|
36
|
+
requires = ["uv_build>=0.9.7,<0.11.0"]
|
|
37
|
+
build-backend = "uv_build"
|
|
38
|
+
|
|
39
|
+
[dependency-groups]
|
|
40
|
+
dev = [
|
|
41
|
+
"bandit>=1.9.4",
|
|
42
|
+
"mypy>=1.19.1",
|
|
43
|
+
"pytest>=9.0.2",
|
|
44
|
+
"pytest-asyncio>=1.3.0",
|
|
45
|
+
"pytest-cov>=7.0.0",
|
|
46
|
+
"ruff>=0.15.5",
|
|
47
|
+
"semgrep>=1.156.0",
|
|
48
|
+
"zizmor>=1.23.1",
|
|
49
|
+
{include-group = "docs"},
|
|
50
|
+
]
|
|
51
|
+
docs = [
|
|
52
|
+
"mkdocs>=1.6.1",
|
|
53
|
+
"mkdocs-material>=9.7.5",
|
|
54
|
+
"mkdocstrings[python]>=1.0.3",
|
|
55
|
+
]
|
|
56
|
+
|
|
57
|
+
[tool.mypy]
|
|
58
|
+
strict = true
|
|
59
|
+
files = ["src", "tests"]
|
|
60
|
+
python_version = "3.10"
|
|
61
|
+
|
|
62
|
+
[tool.ruff]
|
|
63
|
+
src = ["src"]
|
|
64
|
+
include = ["src/**.py", "tests/**.py"]
|
|
65
|
+
target-version = "py310"
|
|
66
|
+
line-length = 120
|
|
67
|
+
|
|
68
|
+
[tool.ruff.lint]
|
|
69
|
+
select = ["ALL"]
|
|
70
|
+
ignore = ["D", "COM812", "TC001"]
|
|
71
|
+
|
|
72
|
+
[tool.ruff.lint.per-file-ignores]
|
|
73
|
+
"tests/**" = ["S101", "D", "PLR2004", "SLF001", "FBT001"]
|
|
74
|
+
"src/zmqtt/_internal/protocol.py" = ["SLF001", "PLR2004"]
|
|
75
|
+
"src/zmqtt/_internal/packets/codec.py" = ["PLR", "C901"]
|
|
76
|
+
"src/zmqtt/client.py" = ["PLR0913", "SLF001", "FBT"]
|
|
77
|
+
|
|
78
|
+
[tool.pytest.ini_options]
|
|
79
|
+
asyncio_mode = "auto"
|
|
80
|
+
addopts = "--strict-markers"
|
|
81
|
+
log_cli_level = "DEBUG"
|
|
82
|
+
markers = [
|
|
83
|
+
"broker: requires a live MQTT broker (run with --broker or docker compose up artemis)",
|
|
84
|
+
]
|
|
85
|
+
|
|
86
|
+
[tool.semantic_release]
|
|
87
|
+
version_toml = ["pyproject.toml:project.version"]
|
|
88
|
+
allow_zero_version = true
|
|
89
|
+
|
|
90
|
+
[tool.semantic_release.commit_parser_options]
|
|
91
|
+
minor_tags = []
|
|
92
|
+
major_tags = ["feat!"]
|
|
93
|
+
patch_tags = ["fix", "feat", "perf", "chore"]
|
|
94
|
+
|
|
95
|
+
[tool.semantic_release.branches.main]
|
|
96
|
+
match = "master"
|
|
97
|
+
prerelease_token = "alpha"
|
|
98
|
+
prerelease = false
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
from zmqtt._internal.packets.properties import AuthProperties, ConnectProperties, PublishProperties
|
|
2
|
+
from zmqtt._internal.types.message import Message
|
|
3
|
+
from zmqtt._internal.types.qos import QoS
|
|
4
|
+
from zmqtt._internal.types.retain_handling import RetainHandling
|
|
5
|
+
from zmqtt.client import (
|
|
6
|
+
MQTTClient,
|
|
7
|
+
MQTTClientV5,
|
|
8
|
+
MQTTClientV311,
|
|
9
|
+
ReconnectConfig,
|
|
10
|
+
Subscription,
|
|
11
|
+
create_client,
|
|
12
|
+
)
|
|
13
|
+
from zmqtt.errors import (
|
|
14
|
+
MQTTConnectError,
|
|
15
|
+
MQTTDisconnectedError,
|
|
16
|
+
MQTTError,
|
|
17
|
+
MQTTProtocolError,
|
|
18
|
+
MQTTTimeoutError,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
__all__ = (
|
|
22
|
+
"AuthProperties",
|
|
23
|
+
"ConnectProperties",
|
|
24
|
+
"MQTTClient",
|
|
25
|
+
"MQTTClientV5",
|
|
26
|
+
"MQTTClientV311",
|
|
27
|
+
"MQTTConnectError",
|
|
28
|
+
"MQTTDisconnectedError",
|
|
29
|
+
"MQTTError",
|
|
30
|
+
"MQTTProtocolError",
|
|
31
|
+
"MQTTTimeoutError",
|
|
32
|
+
"Message",
|
|
33
|
+
"PublishProperties",
|
|
34
|
+
"QoS",
|
|
35
|
+
"ReconnectConfig",
|
|
36
|
+
"RetainHandling",
|
|
37
|
+
"Subscription",
|
|
38
|
+
"create_client",
|
|
39
|
+
)
|
|
File without changes
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"""Python version compatibility helpers."""
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
import contextlib
|
|
5
|
+
import sys
|
|
6
|
+
from collections.abc import AsyncGenerator
|
|
7
|
+
|
|
8
|
+
if sys.version_info >= (3, 11):
|
|
9
|
+
|
|
10
|
+
@contextlib.asynccontextmanager
|
|
11
|
+
async def defer_cancellation() -> AsyncGenerator[None, None]:
|
|
12
|
+
"""Temporarily remove pending cancellations, restore them after cleanup."""
|
|
13
|
+
task = asyncio.current_task()
|
|
14
|
+
cancels = task.cancelling() if task else 0
|
|
15
|
+
for _ in range(cancels):
|
|
16
|
+
if task:
|
|
17
|
+
task.uncancel()
|
|
18
|
+
try:
|
|
19
|
+
yield
|
|
20
|
+
finally:
|
|
21
|
+
for _ in range(cancels):
|
|
22
|
+
if task:
|
|
23
|
+
task.cancel()
|
|
24
|
+
|
|
25
|
+
else:
|
|
26
|
+
|
|
27
|
+
@contextlib.asynccontextmanager
|
|
28
|
+
async def defer_cancellation() -> AsyncGenerator[None, None]:
|
|
29
|
+
"""No-op on Python < 3.11: cancelling()/uncancel() are not available."""
|
|
30
|
+
yield
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"""MQTT packet dataclasses."""
|
|
2
|
+
|
|
3
|
+
from zmqtt._internal.packets.auth import Auth
|
|
4
|
+
from zmqtt._internal.packets.connect import ConnAck, Connect, Will
|
|
5
|
+
from zmqtt._internal.packets.disconnect import Disconnect
|
|
6
|
+
from zmqtt._internal.packets.ping import PingReq, PingResp
|
|
7
|
+
from zmqtt._internal.packets.properties import (
|
|
8
|
+
AuthProperties,
|
|
9
|
+
ConnAckProperties,
|
|
10
|
+
ConnectProperties,
|
|
11
|
+
DisconnectProperties,
|
|
12
|
+
PubAckProperties,
|
|
13
|
+
PublishProperties,
|
|
14
|
+
SubAckProperties,
|
|
15
|
+
SubscribeProperties,
|
|
16
|
+
UnsubAckProperties,
|
|
17
|
+
UnsubscribeProperties,
|
|
18
|
+
WillProperties,
|
|
19
|
+
)
|
|
20
|
+
from zmqtt._internal.packets.publish import PubAck, PubComp, Publish, PubRec, PubRel
|
|
21
|
+
from zmqtt._internal.packets.subscribe import (
|
|
22
|
+
SubAck,
|
|
23
|
+
Subscribe,
|
|
24
|
+
SubscriptionRequest,
|
|
25
|
+
UnsubAck,
|
|
26
|
+
Unsubscribe,
|
|
27
|
+
)
|
|
28
|
+
from zmqtt._internal.packets.types import FixedHeader, Packet, PacketType
|
|
29
|
+
|
|
30
|
+
__all__ = [
|
|
31
|
+
"Auth",
|
|
32
|
+
"AuthProperties",
|
|
33
|
+
"ConnAck",
|
|
34
|
+
"ConnAckProperties",
|
|
35
|
+
"Connect",
|
|
36
|
+
"ConnectProperties",
|
|
37
|
+
"Disconnect",
|
|
38
|
+
"DisconnectProperties",
|
|
39
|
+
"FixedHeader",
|
|
40
|
+
"Packet",
|
|
41
|
+
"PacketType",
|
|
42
|
+
"PingReq",
|
|
43
|
+
"PingResp",
|
|
44
|
+
"PubAck",
|
|
45
|
+
"PubAckProperties",
|
|
46
|
+
"PubComp",
|
|
47
|
+
"PubRec",
|
|
48
|
+
"PubRel",
|
|
49
|
+
"Publish",
|
|
50
|
+
"PublishProperties",
|
|
51
|
+
"SubAck",
|
|
52
|
+
"SubAckProperties",
|
|
53
|
+
"Subscribe",
|
|
54
|
+
"SubscribeProperties",
|
|
55
|
+
"SubscriptionRequest",
|
|
56
|
+
"UnsubAck",
|
|
57
|
+
"UnsubAckProperties",
|
|
58
|
+
"Unsubscribe",
|
|
59
|
+
"UnsubscribeProperties",
|
|
60
|
+
"Will",
|
|
61
|
+
"WillProperties",
|
|
62
|
+
]
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"""Internal wire-format helpers: variable-length integers, UTF-8 strings, binary data."""
|
|
2
|
+
|
|
3
|
+
import struct
|
|
4
|
+
from typing import Final
|
|
5
|
+
|
|
6
|
+
_MAX_MQTT_VARINT: Final = 268_435_455
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def encode_varint(value: int) -> bytes:
|
|
10
|
+
"""Encode a non-negative integer as MQTT variable-length integer (1-4 bytes)."""
|
|
11
|
+
if value < 0 or value > _MAX_MQTT_VARINT:
|
|
12
|
+
msg = f"Value {value} out of varint range"
|
|
13
|
+
raise ValueError(msg)
|
|
14
|
+
result = bytearray()
|
|
15
|
+
while True:
|
|
16
|
+
byte = value & 0x7F
|
|
17
|
+
value >>= 7
|
|
18
|
+
if value:
|
|
19
|
+
byte |= 0x80
|
|
20
|
+
result.append(byte)
|
|
21
|
+
if not value:
|
|
22
|
+
break
|
|
23
|
+
return bytes(result)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def decode_varint(buf: bytes | memoryview, offset: int = 0) -> tuple[int, int]:
|
|
27
|
+
"""Decode MQTT variable-length integer.
|
|
28
|
+
|
|
29
|
+
Returns (value, bytes_consumed). Raises ValueError if buffer is incomplete
|
|
30
|
+
or the encoding exceeds 4 bytes.
|
|
31
|
+
"""
|
|
32
|
+
multiplier = 1
|
|
33
|
+
value = 0
|
|
34
|
+
for i in range(4):
|
|
35
|
+
pos = offset + i
|
|
36
|
+
if pos >= len(buf):
|
|
37
|
+
msg = "Buffer too short for varint"
|
|
38
|
+
raise ValueError(msg)
|
|
39
|
+
byte = buf[pos]
|
|
40
|
+
value += (byte & 0x7F) * multiplier
|
|
41
|
+
if byte & 0x80 == 0:
|
|
42
|
+
return value, i + 1
|
|
43
|
+
multiplier *= 128
|
|
44
|
+
msg = "Varint exceeds 4 bytes"
|
|
45
|
+
raise ValueError(msg)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def encode_str(s: str) -> bytes:
|
|
49
|
+
encoded = s.encode("utf-8")
|
|
50
|
+
return struct.pack("!H", len(encoded)) + encoded
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def decode_str(buf: bytes | memoryview, offset: int) -> tuple[str, int]:
|
|
54
|
+
if offset + 2 > len(buf):
|
|
55
|
+
msg = "Buffer too short for string length prefix"
|
|
56
|
+
raise ValueError(msg)
|
|
57
|
+
(length,) = struct.unpack_from("!H", buf, offset)
|
|
58
|
+
end = offset + 2 + length
|
|
59
|
+
if end > len(buf):
|
|
60
|
+
msg = "Buffer too short for string data"
|
|
61
|
+
raise ValueError(msg)
|
|
62
|
+
return bytes(buf[offset + 2 : end]).decode("utf-8"), 2 + length
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def encode_bytes_field(data: bytes) -> bytes:
|
|
66
|
+
return struct.pack("!H", len(data)) + data
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def decode_bytes_field(buf: bytes | memoryview, offset: int) -> tuple[bytes, int]:
|
|
70
|
+
if offset + 2 > len(buf):
|
|
71
|
+
msg = "Buffer too short for bytes length prefix"
|
|
72
|
+
raise ValueError(msg)
|
|
73
|
+
(length,) = struct.unpack_from("!H", buf, offset)
|
|
74
|
+
end = offset + 2 + length
|
|
75
|
+
if end > len(buf):
|
|
76
|
+
msg = "Buffer too short for bytes data"
|
|
77
|
+
raise ValueError(msg)
|
|
78
|
+
return bytes(buf[offset + 2 : end]), 2 + length
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
from typing import ClassVar
|
|
3
|
+
|
|
4
|
+
from zmqtt._internal.packets.properties import AuthProperties
|
|
5
|
+
from zmqtt._internal.packets.types import Packet, PacketType
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@dataclass(frozen=True, slots=True, kw_only=True)
|
|
9
|
+
class Auth(Packet):
|
|
10
|
+
"""AUTH packet is MQTT 5.0 only, used for enhanced authentication."""
|
|
11
|
+
|
|
12
|
+
packet_type: ClassVar[PacketType] = PacketType.AUTH
|
|
13
|
+
|
|
14
|
+
reason_code: int = 0x00 # 0x00 = success, 0x18 = continue, 0x19 = re-authenticate
|
|
15
|
+
properties: AuthProperties | None = None
|