Skip to main content

No project description provided

Project description

Sniffs - Python MQTT Router

Sniffs is a lightweight Python package for routing MQTT messages using flexible topic patterns. It provides an easy-to-use interface for defining routes and handling incoming messages efficiently.

Features

  • Define routes with dynamic topic patterns
  • Support for named and unnamed placeholders in topic patterns
  • Integration with MQTT clients for seamless message routing

Installation

You can install Sniffs via pip:

pip install sniffs

Usage

import paho.mqtt.client as mqtt
import ssl
from sniffs import Sniffs

app = Sniffs()

@app.route("<key>:{option_1,option_2}/log")
def incredibly_broad_route(key, topic, message):
    print(f"key: {key}")
    print(f"topic: {topic}")
    print(f"message: {message}")

client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, client_id="client123")
client.username_pw_set("username", "password")
client.tls_set(cert_reqs=ssl.CERT_NONE)
client.connect(
    host="host",
    port=9937,
)

app.bind(client)

client.loop_forever()

Documentation

Named Placeholders

Placeholders can be used in your routes. For example, room here is used as a placeholder and the argument name is injected into the function arguments:

@app.route("<room>:{living_room,kitchen}/temperature")
def receive_temperature_data(room):
    if room == "living_room":
        # do something
    elif room == "kitchen":
        # do something else

Argument injection works by looking up the name of the placeholder, so using a different name is the arguments will not work:

# DOES NOT WORK, DO NOT DO THIS!!!
@app.route("<room>:{living_room,kitchen}/temperature")
def receive_temperature_data(argument_one):
    ...

Wildcard Placeholders

If you want to match on anything, you can create a wildcard placeholder by not specifying any placeholder options. This example effectively matches on the topic of +/temperature:

@app.route("<room>/temperature")
def receive_temperature_data(room):
    ...

You can use any number of named and wildcard placeholders together:

@app.route("<room>/<sensor>:{sensor_1,sensor_2}/<sensor_type>{temperature,humidity}")
def receive_temperature_data(room, sensor, sensor_type):
    ...

topic and message

The topic and message arguments are injected, sort of like pytest fixtures. Do not use them as your keys in your routes, as they are reserved.

topic - The topic on which the message was received. This will be the actual topic name, not the templated route. For instance, a route for <room>:{living_room,kitchen}/temperature will always have a topic that is one of the following: living_room/temperature, kitchen/temperature. This maps to the on_message function from paho mqtt client, with the value of msg.topic.

message - This maps to the on_message function, with the value of msg.payload.

@app.route("<room>:{living_room,kitchen}/temperature")
def receive_temperature_data(room, topic, message):
    ...

The arguments are optional, they do not need to be included in your arguments:

@app.route("<room>:{living_room,kitchen}/temperature")
def receive_temperature_data(room):
    ...

Contributing

Contributions are welcome! Please feel free to open issues or submit pull requests.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

sniffs-0.6.3.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sniffs-0.6.3-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file sniffs-0.6.3.tar.gz.

File metadata

  • Download URL: sniffs-0.6.3.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.10 Linux/5.4.0-193-generic

File hashes

Hashes for sniffs-0.6.3.tar.gz
Algorithm Hash digest
SHA256 22598fcd8bffa8ee1361a8f3574fb21661702d40df3a3d3eea1cd4411f7d405b
MD5 13b508ffe144ab38ca0c17558aeb9f16
BLAKE2b-256 c392b9cd2db696e13bb8adebde176e7f602ae3f2fa735b81c16b353b4da55756

See more details on using hashes here.

File details

Details for the file sniffs-0.6.3-py3-none-any.whl.

File metadata

  • Download URL: sniffs-0.6.3-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.10 Linux/5.4.0-193-generic

File hashes

Hashes for sniffs-0.6.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a21ea124b7d63a2c78de642ce5d46820d89a94e321302bfbba491302f512e128
MD5 f59c9e8ee5367de2140063407551bbbd
BLAKE2b-256 b3975956da444753a257dd999a1ccdbd0f01a0f2bf89c06293485c53b888c1a4

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