【发布时间】:2019-03-26 16:29:21
【问题描述】:
我目前正在尝试在面向对象的上下文中使用 Python3 中的 paho mqtt 库。 但是,由于某种原因,回调函数没有被调用。
import paho.mqtt.client as mqtt
import time
import logging
logger = logging.getLogger("IDS_LOGGER.refining")
logging.basicConfig(level=logging.INFO)
class refiner(object):
def __init__(self,configpath="./sampleconfig.xml"):
try:
self.CONFIGPATH = configpath
self.BROKER_IP = "localhost"
self.parse_config()
self.client = mqtt.Client()
self.client.on_connect = self.on_connect
self.client.on_message = self.on_message
self.client.connect(self.BROKER_IP)
logging.info("Connected to {0}, starting MQTT loop".format(self.BROKER_IP))
self.client.loop_forever()
except Exception as e:
print("error")
def on_message(self,client,userdata,msg):
"""MQTT Callback function for handling received messages"""
print("message received!")
def on_connect(self,client,userdata,msg):
print("connected!")
self.client.subscribe("TRACED")
为什么?
【问题讨论】:
标签: python python-3.x oop mqtt