amqtt/samples/client_subscribe_acl.py

45 wiersze
1.5 KiB
Python
Czysty Zwykły widok Historia

2018-03-19 09:58:56 +00:00
import asyncio
import logging
2018-03-19 09:58:56 +00:00
2025-01-12 19:52:50 +00:00
from amqtt.client import ClientError, MQTTClient
2021-03-27 12:59:48 +00:00
from amqtt.mqtt.constants import QOS_1
2018-03-19 09:58:56 +00:00
#
# This sample shows how to subscbribe a topic and receive data from incoming messages
# It subscribes to '$SYS/broker/uptime' topic and displays the first ten values returned
# by the broker.
#
logger = logging.getLogger(__name__)
async def uptime_coro() -> None:
2018-03-19 09:58:56 +00:00
C = MQTTClient()
await C.connect("mqtt://test:test@0.0.0.0:1883")
2020-12-31 00:16:45 +00:00
# await C.connect('mqtt://0.0.0.0:1883')
2018-03-19 09:58:56 +00:00
# Subscribe to '$SYS/broker/uptime' with QOS=1
await C.subscribe(
[
("data/memes", QOS_1), # Topic allowed
("data/classified", QOS_1), # Topic forbidden
2021-03-27 12:59:48 +00:00
("repositories/amqtt/master", QOS_1), # Topic allowed
("repositories/amqtt/devel", QOS_1), # Topic forbidden
("calendar/amqtt/releases", QOS_1), # Topic allowed
],
)
2018-03-19 09:58:56 +00:00
logger.info("Subscribed")
try:
for _i in range(1, 100):
await C.deliver_message()
await C.unsubscribe(["$SYS/broker/uptime", "$SYS/broker/load/#"])
2018-03-19 09:58:56 +00:00
logger.info("UnSubscribed")
2020-12-31 00:16:45 +00:00
await C.disconnect()
2025-01-12 19:52:50 +00:00
except ClientError as ce:
logger.exception(f"Client exception: {ce}")
2018-03-19 09:58:56 +00:00
if __name__ == "__main__":
2018-03-19 09:58:56 +00:00
formatter = "[%(asctime)s] {%(filename)s:%(lineno)d} %(levelname)s - %(message)s"
logging.basicConfig(level=logging.INFO, format=formatter)
asyncio.get_event_loop().run_until_complete(uptime_coro())