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.3.tar.gz (4.3 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.3-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sniffs-0.5.3.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.1 Windows/10

File hashes

Hashes for sniffs-0.5.3.tar.gz
Algorithm Hash digest
SHA256 24daf22938184a81766b6c8b7c0f5bf441a6dc9830512747b71e279cf4cbe5ce
MD5 8cfdb4317f5cd69d04b9ceb1b72f656c
BLAKE2b-256 5b7ef430c39edb38b47fda65958e75531b0a02a1b2f6bf87eca111856d66e28d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sniffs-0.5.3-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.1 Windows/10

File hashes

Hashes for sniffs-0.5.3-py3-none-any.whl
Algorithm Hash digest
SHA256 62e29aecb2561d64d89b4301bd1248dec16e2e384c9fe5e18128fe0aec84650f
MD5 d4a93c9c46d101cd7e6c6678eccd5c41
BLAKE2b-256 31a5f4304a4172546f8ab971f5d5f66fcedd015ea204819dbb9fe13637fabc77

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