amqtt/samples/client_publish_acl.py

36 wiersze
1.2 KiB
Python
Czysty Zwykły widok Historia

2018-03-19 09:58:56 +00:00
import logging
import asyncio
from hbmqtt.client import MQTTClient, ConnectException
#
# This sample shows how to publish messages to broker using different QOS
# Debug outputs shows the message flows
#
logger = logging.getLogger(__name__)
2020-12-31 00:16:45 +00:00
async def test_coro():
2018-03-19 09:58:56 +00:00
try:
C = MQTTClient()
2020-12-31 00:16:45 +00:00
await C.connect('mqtt://0.0.0.0:1883')
await C.publish('data/classified', b'TOP SECRET', qos=0x01)
await C.publish('data/memes', b'REAL FUN', qos=0x01)
await C.publish('repositories/hbmqtt/master', b'NEW STABLE RELEASE', qos=0x01)
await C.publish('repositories/hbmqtt/devel', b'THIS NEEDS TO BE CHECKED', qos=0x01)
await C.publish('calendar/hbmqtt/releases', b'NEW RELEASE', qos=0x01)
2018-03-19 09:58:56 +00:00
logger.info("messages published")
2020-12-31 00:16:45 +00:00
await C.disconnect()
2018-03-19 09:58:56 +00:00
except ConnectException as ce:
logger.error("Connection failed: %s" % ce)
asyncio.get_event_loop().stop()
if __name__ == '__main__':
formatter = "[%(asctime)s] %(name)s {%(filename)s:%(lineno)d} %(levelname)s - %(message)s"
formatter = "%(message)s"
logging.basicConfig(level=logging.DEBUG, format=formatter)
asyncio.get_event_loop().run_until_complete(test_coro())