我们是否可以使用mosquitto作为代理在MQTT上添加SSL服务器证书,并以不增加的编号建立连接。多个客户?

时间:2019-07-11 08:08:37

标签: python ssl mqtt mosquitto

如何与添加ssl服务器证书的多个客户端建立MQTT连接?在y日志中,没有ssl证书的最大客户端数量为1.BUt。可以很容易地增加客户端数量。这是我的代码的一部分。

class MqttClient(mqtt.Client):

    """Mqtt client inherit from mqtt.Client"""

    ## constructor
    def __init__(self, *arg):
        super(MqttClient, self).__init__(arg[0])
        ## on_publish function
        self.on_publish = OnPublish
        ## on_connect function
        self.on_connect = OnConnect
        ## on_log function
        self.on_log = OnLog

        ## payload
        self.payload = ""

        ## name of client
        self.name = arg[0]

        ## broker ip address
        self.broker_ip = arg[1]

        ## broker port number
        self.broker_port = arg[2]

        ## payload sending time
        self.send_time = 0

        ## payload size
        self.payload_size = 0
        self.broker = "khanchi"

    ## return name of client
    def getName(self):
        return self.name

    ## return payload sending time
    def getSendTime(self):
        return self.send_time

    ## return payload size
    def getPayloadSize(self):
        return self.payload_size

    ## connect to broker
    def ConnectToBroker(self):
        try:
            print("connecting to broker(" + self.broker_ip +")")
            self.connect(self.broker_ip, self.broker_port)
            self.loop_start()
            return True
        except Exception as Error:
            print(Error)
            self.disconnect()
            return False

    ## start sending payload until client get disconnected
    def start(self):
        try:
            while True:
                self.payload += read_file("p.txt")
                self.send_time = int(os.popen("date +%s").read().strip())
                self.payload_size = sys.getsizeof(self.payload)
                print(sys.getsizeof(self.payload))
                self.publish("/", self.payload)
                time.sleep(delay)
        except Exception as Error:
            print(Error)
            self.disconnect()


class Mythread(threading.Thread):
    """Mythread inherit from threading.Thread
    use to create thread for mqtt-client
    """
    def __init__(self, client):
        super (Mythread, self).__init__()
        self.client = client

    ## run 
    def run(self):
        self.client.tls_set('/etc/mosquitto/certs/ca.crt')
        self.client.tls_insecure_set(True)
        if self.client.ConnectToBroker():
            self.client.start()

0 个答案:

没有答案