server/mock-actor/mock-actor.py

28 lines
1.1 KiB
Python
Raw Normal View History

2021-08-18 16:48:17 +00:00
import paho.mqtt.client as mqtt
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
print("Subscribing to topic","mlmAccess/pair/response/actor")
print(client.subscribe("mlmAccess/pair/response/actor"))
print("Publishing message to topic","mlmAccess/pair/request/actor")
print(client.publish("mlmAccess/pair/request/actor", "101"))
def on_message(client, userdata, message):
print("message received " ,str(message.payload.decode("utf-8")))
print("message topic=",message.topic)
print("message qos=",message.qos)
print("message retain flag=",message.retain)
client = mqtt.Client(client_id="pair-actor")
client.username_pw_set("pair-actor", "pair-actor")
client.on_connect = on_connect
client.on_message = on_message
print("connecting to broker")
print(client.connect("localhost", 1883, 60))
client.loop_forever()