mqttCore.py 文件源码

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

项目:cloudcam 作者: revmischa 项目源码 文件源码
def connect(self, keepAliveInterval=30):
        if keepAliveInterval is None :
            self._log.error("connect: None type inputs detected.")
            raise TypeError("None type inputs detected.")
        if not isinstance(keepAliveInterval, int):
            self._log.error("connect: Wrong input type detected. KeepAliveInterval must be an integer.")
            raise TypeError("Non-integer type inputs detected.")
        # Return connect succeeded/failed
        ret = False
        # TLS configuration
        if self._useWebsocket:
            # History issue from Yun SDK where AR9331 embedded Linux only have Python 2.7.3 
            # pre-installed. In this version, TLSv1_2 is not even an option.
            # SSLv23 is a work-around which selects the highest TLS version between the client
            # and service. If user installs opensslv1.0.1+, this option will work fine for Mutal
            # Auth.
            # Note that we cannot force TLSv1.2 for Mutual Auth. in Python 2.7.3 and TLS support
            # in Python only starts from Python2.7.
            # See also: https://docs.python.org/2/library/ssl.html#ssl.PROTOCOL_SSLv23
            self._pahoClient.tls_set(ca_certs=self._cafile, cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_SSLv23)
            self._log.info("Connection type: Websocket")
        else:
            self._pahoClient.tls_set(self._cafile, self._cert, self._key, ssl.CERT_REQUIRED, ssl.PROTOCOL_SSLv23)  # Throw exception...
            self._log.info("Connection type: TLSv1.2 Mutual Authentication")
        # Connect
        self._pahoClient.connect(self._host, self._port, keepAliveInterval)  # Throw exception...
        self._pahoClient.loop_start()
        TenmsCount = 0
        while(TenmsCount != self._connectdisconnectTimeout * 100 and self._connectResultCode == sys.maxsize):
            TenmsCount += 1
            time.sleep(0.01)
        if(self._connectResultCode == sys.maxsize):
            self._log.error("Connect timeout.")
            self._pahoClient.loop_stop()
            raise connectTimeoutException()
        elif(self._connectResultCode == 0):
            ret = True
            self._log.info("Connected to AWS IoT.")
            self._log.debug("Connect time consumption: " + str(float(TenmsCount) * 10) + "ms.")
        else:
            self._log.error("A connect error happened: " + str(self._connectResultCode))
            self._pahoClient.loop_stop()
            raise connectError(self._connectResultCode)
        return ret
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号