Code Monkey home page Code Monkey logo

mqttools's Introduction

MQTT Tools

MQTT tools in Python 3.7 and later.

Both the client and the broker implements MQTT version 5.0 using asyncio.

Client features:

  • Subscribe to and publish QoS level 0 topics.
  • Broker session resume (or clean start support) for less initial communication.
  • Topic aliases for smaller publish packets.
  • monitor, subscribe and publish command line commands.

Broker features:

  • Subscribe to and publish QoS level 0 topics.
  • Session resume (or clean start support) for less initial communication. Session state storage in RAM.
  • broker command line command.

Limitations:

There are lots of limitations in both the client and the broker. Here are a few of them:

  • QoS level 1 and 2 messages are not supported. A session state storage is required to do so, both in the client and the broker.
  • Authentication is not supported.

MQTT version 5.0 specification: https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html

Project homepage: https://github.com/eerimoq/mqttools

Documentation: https://mqttools.readthedocs.io

Installation

pip install mqttools

Examples

There are plenty of examples in the examples folder.

Command line

Subscribe

Connect to given MQTT broker and subscribe to a topic. All received messages are printed to standard output.

$ mqttools subscribe /test/#
Connecting to 'localhost:1883'.
Connected.
Topic:   /test
Message: 11
Topic:   /test/mqttools/foo
Message: bar

Publish

Connect to given MQTT broker and publish a message to a topic.

$ mqttools publish /test/mqttools/foo bar
Connecting to 'localhost:1883'.

Published 1 message(s) in 0 seconds from 1 concurrent task(s).

Publish multiple messages as quickly as possible with --count to benchmark the client and the broker.

$ mqttools publish --count 100 /test/mqttools/foo
Connecting to 'localhost:1883'.

Published 100 message(s) in 0.39 seconds from 10 concurrent task(s).

Monitor

Connect to given MQTT broker and monitor given topics in a text based user interface.

$ mqttools monitor /test/#

image

The menu at the bottom of the monitor shows the available commands.

  • Quit: Quit the monitor. Ctrl-C can be used as well.
  • Play/Pause: Toggle between playing and paused (or running and freezed).
  • Format: Message formatting; auto, binary or text.

Broker

Start a broker to serve clients.

$ mqttools broker

Scripting

Subscribe

An example connecting to an MQTT broker, subscribing to the topic /test/#, and printing all published messaged.

import asyncio
import mqttools

async def subscriber():
    client = mqttools.Client('localhost', 1883)

    await client.start()
    await client.subscribe('/test/#')

    while True:
        message = await client.messages.get()

        if message is None:
            print('Broker connection lost!')
            break

        print(f'Topic:   {message.topic}')
        print(f'Message: {message.message}')

asyncio.run(subscriber())

Publish

An example connecting to an MQTT broker and publishing the message bar to the topic /test/mqttools/foo.

import asyncio
import mqttools

async def publisher():
    async with mqttools.Client('localhost', 1883) as client:
        client.publish(mqttools.Message('/test/mqttools/foo', b'bar'))

asyncio.run(publisher())

mqttools's People

Contributors

eerimoq avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

mqttools's Issues

String formatting logging error

The result of asyncio BaseTransport.get_extra_info('peername') gives a 4-tuple on
MacOS, Python 3.9.9.

This results in an error from the formatting in logging the info.

--- Logging error ---
Traceback (most recent call last):
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/logging/__init__.py", line 1083, in emit
    msg = self.format(record)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/logging/__init__.py", line 927, in format
    return fmt.format(record)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/logging/__init__.py", line 663, in format
    record.message = record.getMessage()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/logging/__init__.py", line 367, in getMessage
    msg = msg % self.args
TypeError: not all arguments converted during string formatting
Call stack:
  File "/Users/jeroen/Projects/mug/broker.py", line 16, in <module>
    main()
  File "/Users/jeroen/Projects/mug/broker.py", line 12, in main
    asyncio.run(broker.serve_forever())
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 629, in run_until_complete
    self.run_forever()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 596, in run_forever
    self._run_once()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 1890, in _run_once
    handle._run()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
  File "/Users/jeroen/Projects/mug/venv/lib/python3.9/site-packages/mqttools/broker.py", line 455, in serve_client
    await client.serve_forever()
  File "/Users/jeroen/Projects/mug/venv/lib/python3.9/site-packages/mqttools/broker.py", line 101, in serve_forever
    self.log_info('Serving client %s:%d.', *addr)
  File "/Users/jeroen/Projects/mug/venv/lib/python3.9/site-packages/mqttools/broker.py", line 349, in log_info
    LOGGER.info(fmt, *args)
Message: 'Serving client %s:%d.'
Arguments: ('::1', 50995, 0, 0)

Filter retain messages?

topic, message = await client.messages.get()

How can I filter retain messages? "retain" property of message or something?

Support for response topic

As I was experimenting with mqtt I wanted to see if I could get a request/response pattern working as MQTTv5 introduced. Mqttools is the only v5 implementation I could find with both client and broker implementation that was python only and small enough to understand at least partially.

I found out that message properties were not published completely because the packing of the publish methods did not support properties yet. I experimented a bit on your code and made a small change and got a simple req/rep working.

I have limited experience using github, but if you are interested I could try to push my local branch (or make a pull request, ?)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.