mqttwrapper
A little glue package to make it simple to quickly put together scripts that bridge MQTT and other libraries. See examples below.
Installation
Install from PyPI:
$ pip install mqttwrapper
paho-mqtt is used as the underlying MQTT library.
Usage
mqttwrapper.run_script handles the setup/maintenance of the MQTT connection and subscriptions to topics, and calls a callback function when messages are received. It can run in blocking mode where it will take care of the MQTT client handling or in non blocking mode where a new thread takes care this leaving your main thread free for other things.
The simplest script looks something like this:
from mqttwrapper import run_script
def callback(topic, payload):
print("Received payload {} on topic {}".format(payload, topic))
def main():
run_script(callback, broker="mqtt://127.0.0.1", topics=["my/awesome/topic1", "another/awesome/topic2"])
or in non blocking mode:
def main():
run_script(callback, broker="mqtt://127.0.0.1", topics=["my/awesome/topic1", "another/awesome/topic2"], blocking=False)
while True:
echo "Alive and kicking"
time.sleep(1)
Most extra keyword arguments passed to run_script are passed back to the callback function when it’s called:
from mqttwrapper import run_script
def callback(topic, payload, context, foo):
assert context == "hello"
assert foo == "bar"
print("Received payload {} on topic {}".format(payload, topic))
def main():
run_script(callback, broker="mqtt://127.0.0.1", topics=["my/awesome/topic1", "another/awesome/topic2"], context="hello", foo="bar")
However some are reserved and will be consumed by `run_script` to configure how the MQTT connection behaves:
broker (default MQTT_BROKER from environment, otherwise None): broker URL, e.g mybroker, mqtts://securebroker:8884, mqtt://username:password@coolbroker.example.org
topics (default MQTT_TOPICS from environment, otherwise None): list of topics to subscribe to
ignore_retained (default False): whether to ignore a received message if it was marked as retained
blocking (default True): run in blocking or threaded mode
client_id (default "",): client ID to use when connecting to broker
clean_session (default True): whether to use a clean session upon (re)connect, or resume previous session. client_id must be provided if True
qos (default 0): QoS to use for connection
raise_exceptions (default False): whether to raise exceptions that occur during callback execution
If you don’t need any context passed to your callback, just the topic and payload of messages, you can run in ‘importless’ mode by creating a callback module which has a callback function defined, then execute the mqttwrapper.run module:
$ cat callback.py
def callback(topic, payload):
print(topic, payload)
$ python -m mqttwrapper.run
Retained messages
You can ignore MQTT retained messages by passing ignore_retained=True when calling run_script.
Publishing messages from the callback
Sometimes your callback function might want to publish its own MQTT messages, perhaps in reply to or an acknowledgement of a received message. This is possible by returning an iterable of (topic, payload) tuples from the callback, e.g.:
def callback(topic, payload):
print("Received payload {} on topic {}".format(payload, topic))
return [
("{}/ack".format(topic), payload)
]
mqttwrapper will take care of publishing these messages to the broker.
These tuples can optionally take the form (topic, payload, True) if you want the published message to be marked as retained.
NB any exceptions raised during execution of the callback will be logged and ignored, unless raise_exceptions=True is passed to run_script.
Configuration
broker and topics can be omitted from the run_script call and environment variables used instead:
MQTT_BROKER: MQTT broker to connect to, e.g. mqtt://127.0.0.1/
MQTT_TOPICS: Comma-separated list of topics to subscribe to, e.g. my/topic1,my/topic2
Examples
Metadata
Release files for mqttwrapper 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| mqttwrapper-0.3.0.tar.gz | 5.3 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mqttwrapper-0.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.1 kB
Release files / mqttwrapper-0.3.0.tar.gz
| Download URL | mqttwrapper-0.3.0.tar.gz |
|---|---|
| Size | 5.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d21d1b89d118fa7fab928cc48d92a6b90bad4dae1969ec030783e8be63ad2f51
|
|
BLAKE2b-256 checksum How to use checksums |
f08f187500eb9c569d12866e73cf3bbc8fca82092c68b0725aaa8c576c3b7f3d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / mqttwrapper-0.3.0-py3-none-any.whl
| Download URL | mqttwrapper-0.3.0-py3-none-any.whl |
|---|---|
| Size | 5.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
224b458ef1d9115eed67831010f187fb72eb24aadb883c6211e19c8ecf001e17
|
|
BLAKE2b-256 checksum How to use checksums |
b292a69287d88f51c79746cf87b8c6953d81ea9ad433dfb3ca89ceb2f494e49a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Security for mqttwrapper 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.