2018-03-19 09:58:56 +00:00
|
|
|
import asyncio
|
2024-12-19 19:34:09 +00:00
|
|
|
import logging
|
2018-03-19 09:58:56 +00:00
|
|
|
|
2025-01-12 19:52:50 +00:00
|
|
|
from amqtt.client import ConnectError, MQTTClient
|
2025-06-17 21:03:40 +00:00
|
|
|
from amqtt.mqtt.constants import QOS_1
|
2018-03-19 09:58:56 +00:00
|
|
|
|
2025-06-17 21:03:40 +00:00
|
|
|
"""
|
|
|
|
This sample shows how to publish messages to broker running either `samples/broker_acl.py`
|
|
|
|
or `samples/broker_taboo.py`.
|
|
|
|
"""
|
2018-03-19 09:58:56 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2024-12-19 19:34:09 +00:00
|
|
|
async def test_coro() -> None:
|
2018-03-19 09:58:56 +00:00
|
|
|
try:
|
2025-06-17 21:03:40 +00:00
|
|
|
client = MQTTClient()
|
|
|
|
await client.connect("mqtt://0.0.0.0:1883")
|
|
|
|
await client.publish("data/classified", b"TOP SECRET", qos=QOS_1)
|
|
|
|
await client.publish("data/memes", b"REAL FUN", qos=QOS_1)
|
|
|
|
await client.publish("repositories/amqtt/master", b"NEW STABLE RELEASE", qos=QOS_1)
|
|
|
|
await client.publish(
|
2024-12-19 19:34:09 +00:00
|
|
|
"repositories/amqtt/devel",
|
|
|
|
b"THIS NEEDS TO BE CHECKED",
|
2025-06-17 21:03:40 +00:00
|
|
|
qos=QOS_1,
|
2021-03-14 21:16:51 +00:00
|
|
|
)
|
2025-06-17 21:03:40 +00:00
|
|
|
await client.publish("calendar/amqtt/releases", b"NEW RELEASE", qos=QOS_1)
|
2018-03-19 09:58:56 +00:00
|
|
|
logger.info("messages published")
|
2025-06-17 21:03:40 +00:00
|
|
|
await client.disconnect()
|
2025-01-12 19:52:50 +00:00
|
|
|
except ConnectError as ce:
|
2025-06-17 21:03:40 +00:00
|
|
|
logger.exception("ERROR: Connection failed")
|
2018-03-19 09:58:56 +00:00
|
|
|
|
|
|
|
|
2025-06-17 21:03:40 +00:00
|
|
|
def __main__():
|
|
|
|
|
|
|
|
formatter = "[%(asctime)s] :: %(levelname)s :: %(name)s :: %(message)s"
|
|
|
|
logging.basicConfig(level=logging.INFO, format=formatter)
|
|
|
|
|
|
|
|
asyncio.run(test_coro())
|
|
|
|
|
2021-03-14 21:16:51 +00:00
|
|
|
if __name__ == "__main__":
|
2025-06-17 21:03:40 +00:00
|
|
|
__main__()
|