|
49 | 49 | from AWSIoTPythonSDK.core.protocol.connection.cores import SecuredWebSocketCore |
50 | 50 | from AWSIoTPythonSDK.core.protocol.connection.alpn import SSLContextBuilder |
51 | 51 |
|
52 | | -VERSION_MAJOR=1 |
53 | | -VERSION_MINOR=0 |
54 | | -VERSION_REVISION=0 |
55 | | -VERSION_NUMBER=(VERSION_MAJOR*1000000+VERSION_MINOR*1000+VERSION_REVISION) |
56 | | - |
57 | 52 | MQTTv31 = 3 |
58 | 53 | MQTTv311 = 4 |
59 | 54 |
|
@@ -924,6 +919,9 @@ def loop(self, timeout=1.0, max_packets=1): |
924 | 919 | # Can occur if we just reconnected but rlist/wlist contain a -1 for |
925 | 920 | # some reason. |
926 | 921 | return MQTT_ERR_CONN_LOST |
| 922 | + except KeyboardInterrupt: |
| 923 | + # Allow ^C to interrupt |
| 924 | + raise |
927 | 925 | except: |
928 | 926 | return MQTT_ERR_UNKNOWN |
929 | 927 |
|
@@ -981,6 +979,9 @@ def publish(self, topic, payload=None, qos=0, retain=False): |
981 | 979 | raise ValueError('Invalid QoS level.') |
982 | 980 | if isinstance(payload, str) or isinstance(payload, bytearray): |
983 | 981 | local_payload = payload |
| 982 | + # Client.publish() now accepts bytes() payloads on Python 3. |
| 983 | + elif sys.version_info[0] == 3 and isinstance(payload, bytes): |
| 984 | + local_payload = bytearray(payload) |
984 | 985 | elif sys.version_info[0] < 3 and isinstance(payload, unicode): |
985 | 986 | local_payload = payload |
986 | 987 | elif isinstance(payload, int) or isinstance(payload, float): |
@@ -1117,7 +1118,8 @@ def subscribe(self, topic, qos=0): |
1117 | 1118 | zero string length, or if topic is not a string, tuple or list. |
1118 | 1119 | """ |
1119 | 1120 | topic_qos_list = None |
1120 | | - if isinstance(topic, str): |
| 1121 | + # Client.subscribe() now accepts unicode type topic inputs on Python 2 |
| 1122 | + if isinstance(topic, str) or (sys.version_info[0] == 2 and isinstance(topic, unicode)): |
1121 | 1123 | if qos<0 or qos>2: |
1122 | 1124 | raise ValueError('Invalid QoS level.') |
1123 | 1125 | if topic is None or len(topic) == 0: |
@@ -1165,7 +1167,8 @@ def unsubscribe(self, topic): |
1165 | 1167 | topic_list = None |
1166 | 1168 | if topic is None: |
1167 | 1169 | raise ValueError('Invalid topic.') |
1168 | | - if isinstance(topic, str): |
| 1170 | + # Client.unsubscribe() now accepts unicode type topic inputs on Python 2 |
| 1171 | + if isinstance(topic, str) or (sys.version_info[0] == 2 and isinstance(topic, unicode)): |
1169 | 1172 | if len(topic) == 0: |
1170 | 1173 | raise ValueError('Invalid topic.') |
1171 | 1174 | topic_list = [topic.encode('utf-8')] |
@@ -1453,8 +1456,10 @@ def loop_stop(self, force=False): |
1453 | 1456 | return MQTT_ERR_INVAL |
1454 | 1457 |
|
1455 | 1458 | self._thread_terminate = True |
1456 | | - self._thread.join() |
1457 | | - self._thread = None |
| 1459 | + # Don't attempt to join() own thread. |
| 1460 | + if threading.current_thread() != self._thread: |
| 1461 | + self._thread.join() |
| 1462 | + self._thread = None |
1458 | 1463 |
|
1459 | 1464 | def message_callback_add(self, sub, callback): |
1460 | 1465 | """Register a message callback for a specific topic. |
@@ -1704,6 +1709,10 @@ def _easy_log(self, level, buf): |
1704 | 1709 | self.on_log(self, self._userdata, level, buf) |
1705 | 1710 |
|
1706 | 1711 | def _check_keepalive(self): |
| 1712 | + # Fix for keepalive=0 causing an infinite disconnect/reconnect loop. |
| 1713 | + if self._keepalive == 0: |
| 1714 | + return MQTT_ERR_SUCCESS |
| 1715 | + |
1707 | 1716 | now = time.time() |
1708 | 1717 | self._msgtime_mutex.acquire() |
1709 | 1718 | last_msg_out = self._last_msg_out |
|
0 commit comments