mqtt_client_shell.py 文件源码

python
阅读 17 收藏 0 点赞 0 评论 0

项目:python-mqtt-client-shell 作者: bapowell 项目源码 文件源码
def do_connect(self, arg):
        """Connect to the MQTT server, using the current connection parameters.
        If connection is successful, then go to the Messaging console."""
        connected = None
        ca = self.context.connection_args
        try:        
            if ca.will:
                self.context.mqttclient.will_set(topic=ca.will.topic, payload=ca.will.payload, qos=ca.will.qos, retain=ca.will.retain)

            if ca.username:
                self.context.mqttclient.username_pw_set(ca.username, ca.password)
            else:
                self.context.mqttclient.username_pw_set("", None)

            if ca.tls_args.ca_certs_filepath:
                ta = ca.tls_args
                self.context.mqttclient.tls_set(ca_certs=ta.ca_certs_filepath, certfile=ta.cert_filepath, keyfile=ta.key_filepath,
                                                cert_reqs=ta.cert_reqs, tls_version=ta.tls_version, ciphers=ta.ciphers)
                self.context.mqttclient.tls_insecure_set(ta.tls_insecure)

            rc = self.context.mqttclient.connect(host=ca.host, port=ca.port, keepalive=ca.keepalive, bind_address=ca.bind_address)
            connected = (rc == mqtt.MQTT_ERR_SUCCESS)
        except Exception as e:
            print(e)
        else:
            if not connected:
                print("Unable to connect")
            else:
                self.context.mqttclient.loop_start()
                # Initiate the "Messaging" console.
                MessagingConsole(self.context).cmdloop()
                # Clean up (disconnect), after returning from "Messaging" console.
                self.context.mqttclient.disconnect()
                connected = False
                self.context.mqttclient.loop_stop()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号