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.5.4.tar.gz (4.6 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.5.4-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sniffs-0.5.4.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.0 CPython/3.10.10 Linux/5.4.0-174-generic

File hashes

Hashes for sniffs-0.5.4.tar.gz
Algorithm Hash digest
SHA256 d56b5018c2e4233fb1cdba8e51e73afc1dba7fc118e647c3834b00d0f424f786
MD5 1b9f2e0df50d11e82d5821e238f58b75
BLAKE2b-256 7685e54f8c06eab2d132d661d1d49cff4390754a65641d658f6024dae9d4bfac

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for sniffs-0.5.4-py3-none-any.whl
Algorithm Hash digest
SHA256 83eb21c0d566ef5029dbee5e2faf8ad8ed897f74574590b2a21b6bdbfb65df77
MD5 9045a22168991b9bdfff7db0040c791c
BLAKE2b-256 73931c6c1de507be266c79b2d294b946f1be5e0d99506736789df3e22b8e7c64

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