本文整理汇总了Python中paho.mqtt.client.Client方法的典型用法代码示例。如果您正苦于以下问题:Python client.Client方法的具体用法?Python client.Client怎么用?Python client.Client使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类paho.mqtt.client
的用法示例。
在下文中一共展示了client.Client方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: on_connect
# 需要导入模块: from paho.mqtt import client [as 别名]
# 或者: from paho.mqtt.client import Client [as 别名]
def on_connect(self, client, userdata, message, return_code):
"""
Called when the broker responds to our connection request.
@param client:
the client instance for this callback
@param userdata:
the private user data as set in Client() or userdata_set()
@param message:
response message sent by the broker
@param return_code:
the connection result
"""
debuglog(1, "MQTT on_connect({},{},{},{}): {}".format(client, userdata, message, return_code, mqtt.error_string(return_code)))
for topic in self._args.mqtt_topic:
debuglog(1, "subscribe to topic {}".format(topic))
client.subscribe(topic, 0)
示例2: on_message
# 需要导入模块: from paho.mqtt import client [as 别名]
# 或者: from paho.mqtt.client import Client [as 别名]
def on_message(self, client, userdata, message):
"""
Called when a message has been received on a topic that the client subscribes to.
This callback will be called for every message received.
@param client:
the client instance for this callback
@param userdata:
the private user data as set in Client() or userdata_set()
@param message:
an instance of MQTTMessage.
This is a class with members topic, payload, qos, retain.
"""
global EXIT_CODE # pylint: disable=global-statement
if EXIT_CODE != ExitCode.OK:
sys.exit(EXIT_CODE)
log(2, '{} {} [QOS {} Retain {}]'.format(message.topic, message.payload, message.qos, message.retain))
debuglog(2, "on_message({},{},{})".format(client, userdata, message))
if EXIT_CODE == ExitCode.OK:
self.pool_sqlconnections.acquire()
self.write2sql_thread = Thread(target=self.write2sql, args=(message,))
self.write2sql_thread.start()
示例3: on_publish
# 需要导入模块: from paho.mqtt import client [as 别名]
# 或者: from paho.mqtt.client import Client [as 别名]
def on_publish(self, client, userdata, mid):
"""
Called when a message that was to be sent using the publish() call
has completed transmission to the broker.
For messages with QoS levels 1 and 2, this means that the appropriate
handshakes have completed. For QoS 0, this simply means that the
message has left the client. The mid variable matches the mid
variable returned from the corresponding publish() call, to allow
outgoing messages to be tracked.
@param client:
the client instance for this callback
@param userdata:
the private user data as set in Client() or userdata_set()
@param mid:
matches the mid variable returned from the corresponding
publish() call, to allow outgoing messages to be tracked.
"""
debuglog(2, "on_publish({},{},{})".format(client, userdata, mid))
示例4: on_subscribe
# 需要导入模块: from paho.mqtt import client [as 别名]
# 或者: from paho.mqtt.client import Client [as 别名]
def on_subscribe(self, client, userdata, mid, granted_qos):
"""
Called when the broker responds to a subscribe request.
@param client:
the client instance for this callback
@param userdata:
the private user data as set in Client() or userdata_set()
@param mid:
Matches the mid variable returned from the corresponding
subscribe() call.
@param granted_qos:
a list of integers that give the QoS level the broker has
granted for each of the different subscription requests.
"""
debuglog(2, "on_subscribe({},{},{},{})".format(client, userdata, mid, granted_qos))
示例5: on_log
# 需要导入模块: from paho.mqtt import client [as 别名]
# 或者: from paho.mqtt.client import Client [as 别名]
def on_log(self, client, userdata, level, string):
"""
Called when the client has log information.
@param client:
the client instance for this callback
@param userdata:
the private user data as set in Client() or userdata_set()
@param level:
gives the severity of the message and will be one of
MQTT_LOG_INFO, MQTT_LOG_NOTICE, MQTT_LOG_WARNING, MQTT_LOG_ERR,
and MQTT_LOG_DEBUG. The message itself is in string.
@param string:
The message itself
"""
debuglog(2, "on_log({},{},{},{})".format(client, userdata, level, string))
示例6: initialize
# 需要导入模块: from paho.mqtt import client [as 别名]
# 或者: from paho.mqtt.client import Client [as 别名]
def initialize(self):
try:
if DEBUG_ON:
print('connect again')
self._mqttc = mqtt.Client(None)
self._mqttc.on_message = self.mqtt_on_message
self._mqttc.on_connect = self.mqtt_on_connect
self._mqttc.on_subscribe = self.mqtt_on_subscribe
self._mqttc.on_publish = self.mqtt_on_publish
self._mqttc.on_disconnect = self.on_disconnect
# Enable this line if you are doing the snip code, off stress
# self._mqttc.loop_start()
except TypeError:
print('Connect to mqtter error')
return
示例7: __init__
# 需要导入模块: from paho.mqtt import client [as 别名]
# 或者: from paho.mqtt.client import Client [as 别名]
def __init__(self, config, verbose, logger):
"""Initialize an MQTT client.
Args:
config (:class:`.ServerConfig`): The configuration of
the MQTT client.
verbose (bool): Whether or not the MQTT client runs in verbose
mode.
logger (:class:`logging.Logger`): The Logger object for logging
messages.
"""
self.config = config
self.verbose = verbose
self.logger = logger
self.mqtt = Client()
self.logger.debug('Using %s', pyaudio.get_portaudio_version_text())
self.logger.debug('Creating PyAudio object...')
self.audio = pyaudio.PyAudio()
self.initialize()
self.mqtt.on_connect = self.on_connect
self.mqtt.on_disconnect = self.on_disconnect
self.connect()
示例8: connect
# 需要导入模块: from paho.mqtt import client [as 别名]
# 或者: from paho.mqtt.client import Client [as 别名]
def connect(self, host, port, ca_path=None, client_path=None, key_path=None):
self.__client = mqtt.Client(protocol=mqtt.MQTTv311, userdata=self.__user_data)
if ca_path is not None and client_path is not None and key_path is not None:
self.ssl_setting(ca_path, client_path, key_path)
will = self.__user_will
if will is None:
event_loop_message = EventLoop.get_message(EVENT_LOOP.STATE.WILL, self.__pid)
payload = self.__topicPub.serialize(event_loop_message)
will = {"topic": self.__topicPub.get_path(), "payload": payload}
self.__client.will_set(will["topic"], payload=will["payload"], qos=2, retain=False)
self.__client.on_connect = self.__on_connect
self.__client.on_message = self.__on_message
self.__client.connect(host=host, port=port, keepalive=EVENT_LOOP.KEEP_ALIVE)
示例9: __init__
# 需要导入模块: from paho.mqtt import client [as 别名]
# 或者: from paho.mqtt.client import Client [as 别名]
def __init__(self, host=DEFAULT_BROKER_HOST, port=DEFAULT_BROKER_PORT):
self._mqtt_client = mqtt.Client()
self.host = host
self.port = port
self.timeout = 10
self.prefix = TOPIC_PREFIX
self.listen_timeout = 5
self.cli = None
self.should_stop = False
self.keyboard_interrupt_occurred = False
self.loop_lock = Lock()
self._mqtt_client.on_message = self.mqtt_on_message
self._mqtt_client.on_connect = self.mqtt_on_connect
示例10: __init__
# 需要导入模块: from paho.mqtt import client [as 别名]
# 或者: from paho.mqtt.client import Client [as 别名]
def __init__(self, client_id=None, host=DEFAULT_BROKER_HOST, port=DEFAULT_BROKER_PORT, timeout=60, topics=None,
listen_timeout=60, scan_instance=None, cli=None):
"""Active Scanner object initiation"""
self._mqtt_client = mqtt.Client(client_id)
self.cli = cli
if cli:
self.host = cli.mqtt_client.host
self.port = cli.mqtt_client.port
else:
self.host = host
self.port = port
self.timeout = timeout
self.topics = topics
self.listen_timeout = int(listen_timeout)
self.scan_instance = scan_instance
self._base_topic = '$SYS/test123'
self._mqtt_client.on_message = self.mqtt_on_message
示例11: __init__
# 需要导入模块: from paho.mqtt import client [as 别名]
# 或者: from paho.mqtt.client import Client [as 别名]
def __init__(self, host, port=1883, client_id="", client_username="", client_password=None, server_tls=False, server_cert=None, topics=[], mock_class=None):
self.host = host
self.port = port
self.client_id = client_id
self.client_username = client_id
self.client_password = client_password
self.topics = topics
self.server_tls = server_tls
self.server_cert = server_cert
if mock_class:
self.client = MockMQTTClient(self.client_id)
else:
self.client = paho.Client(self.client_id)
if self.client_username:
self.client.username_pw_set(self.client_username, password=self.client_password)
self._connect()
示例12: connectMqtt
# 需要导入模块: from paho.mqtt import client [as 别名]
# 或者: from paho.mqtt.client import Client [as 别名]
def connectMqtt(self):
try:
mqttClient = mqtt.Client()
if self._mqttUsername and self._mqttPassword:
mqttClient.username_pw_set(self._mqttUsername, self._mqttPassword)
mqttClient.on_log = self.onLog
mqttClient.on_connect = self.onConnect
mqttClient.on_message = self.onMessage
if self._tlsFile:
mqttClient.tls_set(certfile=self._tlsFile)
mqttClient.tls_insecure_set(False)
mqttClient.connect(self._mqttServer, int(self._mqttPort))
mqttClient.loop_start()
return mqttClient
except:
self._logger.fatal("Couldn't connect to mqtt, aborting")
self.onStop()
# noinspection PyUnusedLocal
示例13: on_connect
# 需要导入模块: from paho.mqtt import client [as 别名]
# 或者: from paho.mqtt.client import Client [as 别名]
def on_connect(client, userdata, flags, rc):
print("Connected to the {0} topic".
format(topic))
subscribe_result = client.subscribe(topic)
publish_result_1 = client.publish(
topic=topic,
payload="Listening to messages in the Paho Python Client")
publish_result_2 = publish_command(
client,
topic,
"print_temperature_fahrenheit",
"temperature_fahrenheit",
45)
publish_result_3 = publish_command(
client,
topic,
"print_information_message",
"text",
"Python IoT")
示例14: __init__
# 需要导入模块: from paho.mqtt import client [as 别名]
# 或者: from paho.mqtt.client import Client [as 别名]
def __init__(self, message_callback = None):
"""
初始化mqtt客户端
"""
self._logger = logging.getLogger()
self.mqtt_path = device.product_key + ".iot-as-mqtt.cn-shanghai.aliyuncs.com" # MQTT地址
self._topic = topic
# 获取签名
self.sign_dict = Sign.get_sign({
"deviceName": device.device_name,
"productKey": device.product_key
}, device.device_secret)
self.mqtt_client_id = self.sign_dict['iot_client_id'] + \
"|securemode=3,signmethod=hmacsha1,timestamp=" + \
self.sign_dict['timestamp'] + "|"
self._logger.info('use mqtt device id:"%s"', self.mqtt_client_id)
# 实例化mqtt客户端
self.mqttc = mqtt.Client(transport="tcp", client_id=self.mqtt_client_id)
self._iot_server = IotServer.get_instance()
示例15: connect_to_mqtt
# 需要导入模块: from paho.mqtt import client [as 别名]
# 或者: from paho.mqtt.client import Client [as 别名]
def connect_to_mqtt(self):
try:
if DEBUG_ON:
print 'connect again'
self._mqttc = mqtt.Client(None)
self._mqttc.on_message = self.mqtt_on_message
self._mqttc.on_connect = self.mqtt_on_connect
self._mqttc.on_subscribe = self.mqtt_on_subscribe
self._mqttc.on_publish = self.mqtt_on_publish
self._mqttc.on_disconnect = self.on_disconnect
self._mqttc.connect("broker.pikabot.org", 1883, 60)
# Enable this line if you are doing the snip code, off stress
# self._mqttc.loop_start()
except TypeError:
print 'Connect to mqtter error'
return