A small example package
Project description
Mqtt common library
Usage
from MqttLibPy.client import MqttClient
# from MqttLibPy.serializer import Serializer
# Both prefix and postfix are optional
client = MqttClient('myhost.com', 1883, prefix="myprefix")
# This function will be called when a message is received in the myprefix/myendpoint topic
@client.endpoint("myendpoint", force_json=True)
def myendpoint(mqtt_client, _, json_body):
print(json_body)
my_field = json_body["some_field"]
# Do stuff with my_field
# ..
# Return a response
my_response = {"another_field": "Ok!"}
# Sends message to topic "myendpoint"
client.send_message_serialized(my_response, "myendpoint", valid_json=True)
Send file
Sender
client = MqttClient("myhost.com", 1883)
client.send_file("test_bytes", "/path/to/my/file/myfile.")
listener
client = MqttClient("myhost.com", 1883)
@client.endpoint("test_bytes", is_file=True)
def get_file(client, user_data, file):
with open(f"/path/to/save/file/{file['data'][0]['filename']}", 'wb+') as f:
f.write(file['bytes'])
f.close()
threading.Thread(target=client.listen).start()
Changelog
1.0.0
- Adds send file method
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
mqttlibpy-1.0.1.tar.gz
(5.8 kB
view hashes)
Built Distribution
Close
Hashes for mqttlibpy-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7c3dc021f57b9547fe2b9f0c00aab7d30b8c9e798a42069325ce3b1160cb5a65 |
|
MD5 | c41d9975597c23226f0a71ca67715c57 |
|
BLAKE2b-256 | 54b451432eb035779b7620f79879d82b2e580ca96d1388e8e8ed647cd49e9515 |