From 8f1f8974147438533a8ecdbe374793138cbb1fa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=8B=E4=B9=89=E6=B7=B1?= <1371033826@qq.com> Date: Fri, 27 Aug 2021 22:32:03 +0800 Subject: [PATCH 1/4] Update adafruit_minimqtt.py --- adafruit_minimqtt/adafruit_minimqtt.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/adafruit_minimqtt/adafruit_minimqtt.py b/adafruit_minimqtt/adafruit_minimqtt.py index 1c4a0d2..36acd28 100755 --- a/adafruit_minimqtt/adafruit_minimqtt.py +++ b/adafruit_minimqtt/adafruit_minimqtt.py @@ -64,6 +64,13 @@ _default_sock = None # pylint: disable=invalid-name _fake_context = None # pylint: disable=invalid-name +# Override default len() method +len_overrided = len +def len(object): + if isinstance(object, str): + return len_overrided(object.encode('utf-8')) + else: + return len_overrided(object) class MMQTTException(Exception): """MiniMQTT Exception class.""" From 3980b5a41ebd2b701a5c404a23897f2057d1e77e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=8B=E4=B9=89=E6=B7=B1?= <1371033826@qq.com> Date: Sat, 28 Aug 2021 07:43:03 +0800 Subject: [PATCH 2/4] Update adafruit_minimqtt.py --- adafruit_minimqtt/adafruit_minimqtt.py | 31 ++++++++++---------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/adafruit_minimqtt/adafruit_minimqtt.py b/adafruit_minimqtt/adafruit_minimqtt.py index 36acd28..8131fe6 100755 --- a/adafruit_minimqtt/adafruit_minimqtt.py +++ b/adafruit_minimqtt/adafruit_minimqtt.py @@ -64,13 +64,6 @@ _default_sock = None # pylint: disable=invalid-name _fake_context = None # pylint: disable=invalid-name -# Override default len() method -len_overrided = len -def len(object): - if isinstance(object, str): - return len_overrided(object.encode('utf-8')) - else: - return len_overrided(object) class MMQTTException(Exception): """MiniMQTT Exception class.""" @@ -188,7 +181,7 @@ def __init__( randint(0, int(time.monotonic() * 100) % 1000), randint(0, 99) ) # generated client_id's enforce spec.'s length rules - if len(self.client_id) > 23 or not self.client_id: + if len(self.client_id.encode("utf-8")) > 23 or not self.client_id: raise ValueError("MQTT Client ID must be between 1 and 23 bytes") # LWT @@ -457,16 +450,16 @@ def connect(self, clean_session=True, host=None, port=None, keep_alive=None): var_header[6] = clean_session << 1 # Set up variable header and remaining_length - remaining_length = 12 + len(self.client_id) + remaining_length = 12 + len(self.client_id.encode("utf-8")) if self._username: - remaining_length += 2 + len(self._username) + 2 + len(self._password) + remaining_length += 2 + len(self._username.encode("utf-8")) + 2 + len(self._password.encode("utf-8")) var_header[6] |= 0xC0 if self.keep_alive: assert self.keep_alive < MQTT_TOPIC_LENGTH_LIMIT var_header[7] |= self.keep_alive >> 8 var_header[8] |= self.keep_alive & 0x00FF if self._lw_topic: - remaining_length += 2 + len(self._lw_topic) + 2 + len(self._lw_msg) + remaining_length += 2 + len(self._lw_topic.encode("utf-8")) + 2 + len(self._lw_msg.encode("utf-8")) var_header[6] |= 0x4 | (self._lw_qos & 0x1) << 3 | (self._lw_qos & 0x2) << 3 var_header[6] |= self._lw_retain << 5 @@ -583,7 +576,7 @@ def publish(self, topic, msg, retain=False, qos=0): pass else: raise MMQTTException("Invalid message data type.") - if len(msg) > MQTT_MSG_MAX_SZ: + if len(msg.encode("utf-8")) > MQTT_MSG_MAX_SZ: raise MMQTTException("Message size larger than %d bytes." % MQTT_MSG_MAX_SZ) assert ( 0 <= qos <= 1 @@ -593,10 +586,10 @@ def publish(self, topic, msg, retain=False, qos=0): pub_hdr_fixed = bytearray([0x30 | retain | qos << 1]) # variable header = 2-byte Topic length (big endian) - pub_hdr_var = bytearray(struct.pack(">H", len(topic))) + pub_hdr_var = bytearray(struct.pack(">H", len(topic.encode("utf-8")))) pub_hdr_var.extend(topic.encode("utf-8")) # Topic name - remaining_length = 2 + len(msg) + len(topic) + remaining_length = 2 + len(msg.encode("utf-8")) + len(topic.encode("utf-8")) if qos > 0: # packet identifier where QoS level is 1 or 2. [3.3.2.2] remaining_length += 2 @@ -675,7 +668,7 @@ def subscribe(self, topic, qos=0): topics.append((t, q)) # Assemble packet packet_length = 2 + (2 * len(topics)) + (1 * len(topics)) - packet_length += sum(len(topic) for topic, qos in topics) + packet_length += sum(len(topic.encode("utf-8")) for topic, qos in topics) packet_length_byte = packet_length.to_bytes(1, "big") self._pid = self._pid + 1 if self._pid < 0xFFFF else 1 packet_id_bytes = self._pid.to_bytes(2, "big") @@ -683,7 +676,7 @@ def subscribe(self, topic, qos=0): packet = MQTT_SUB + packet_length_byte + packet_id_bytes # attaching topic and QOS level to the packet for t, q in topics: - topic_size = len(t).to_bytes(2, "big") + topic_size = len(t.encode("utf-8")).to_bytes(2, "big") qos_byte = q.to_bytes(1, "big") packet += topic_size + t.encode() + qos_byte if self.logger: @@ -724,13 +717,13 @@ def unsubscribe(self, topic): ) # Assemble packet packet_length = 2 + (2 * len(topics)) - packet_length += sum(len(topic) for topic in topics) + packet_length += sum(len(topic.encode("utf-8")) for topic in topics) packet_length_byte = packet_length.to_bytes(1, "big") self._pid = self._pid + 1 if self._pid < 0xFFFF else 1 packet_id_bytes = self._pid.to_bytes(2, "big") packet = MQTT_UNSUB + packet_length_byte + packet_id_bytes for t in topics: - topic_size = len(t).to_bytes(2, "big") + topic_size = len(t.encode("utf-8")).to_bytes(2, "big") packet += topic_size + t.encode() if self.logger: for t in topics: @@ -921,7 +914,7 @@ def _send_str(self, string): :param str string: String to write to the socket. """ - self._sock.send(struct.pack("!H", len(string))) + self._sock.send(struct.pack("!H", len(string.encode("utf-8")))) if isinstance(string, str): self._sock.send(str.encode(string, "utf-8")) else: From df63c8f40fce16923865130225a1f9b49468fa05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=8B=E4=B9=89=E6=B7=B1?= <1371033826@qq.com> Date: Sat, 28 Aug 2021 08:00:06 +0800 Subject: [PATCH 3/4] Update adafruit_minimqtt.py --- adafruit_minimqtt/adafruit_minimqtt.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_minimqtt/adafruit_minimqtt.py b/adafruit_minimqtt/adafruit_minimqtt.py index 8131fe6..ddea7cb 100755 --- a/adafruit_minimqtt/adafruit_minimqtt.py +++ b/adafruit_minimqtt/adafruit_minimqtt.py @@ -576,7 +576,7 @@ def publish(self, topic, msg, retain=False, qos=0): pass else: raise MMQTTException("Invalid message data type.") - if len(msg.encode("utf-8")) > MQTT_MSG_MAX_SZ: + if len(msg) > MQTT_MSG_MAX_SZ: raise MMQTTException("Message size larger than %d bytes." % MQTT_MSG_MAX_SZ) assert ( 0 <= qos <= 1 @@ -589,7 +589,7 @@ def publish(self, topic, msg, retain=False, qos=0): pub_hdr_var = bytearray(struct.pack(">H", len(topic.encode("utf-8")))) pub_hdr_var.extend(topic.encode("utf-8")) # Topic name - remaining_length = 2 + len(msg.encode("utf-8")) + len(topic.encode("utf-8")) + remaining_length = 2 + len(msg) + len(topic.encode("utf-8")) if qos > 0: # packet identifier where QoS level is 1 or 2. [3.3.2.2] remaining_length += 2 From a2032c06ee3e6e416d766339da038e84d6e01e2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=8B=E4=B9=89=E6=B7=B1?= <1371033826@qq.com> Date: Sat, 28 Aug 2021 08:14:39 +0800 Subject: [PATCH 4/4] Update adafruit_minimqtt.py --- adafruit_minimqtt/adafruit_minimqtt.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/adafruit_minimqtt/adafruit_minimqtt.py b/adafruit_minimqtt/adafruit_minimqtt.py index ddea7cb..35470c3 100755 --- a/adafruit_minimqtt/adafruit_minimqtt.py +++ b/adafruit_minimqtt/adafruit_minimqtt.py @@ -459,7 +459,7 @@ def connect(self, clean_session=True, host=None, port=None, keep_alive=None): var_header[7] |= self.keep_alive >> 8 var_header[8] |= self.keep_alive & 0x00FF if self._lw_topic: - remaining_length += 2 + len(self._lw_topic.encode("utf-8")) + 2 + len(self._lw_msg.encode("utf-8")) + remaining_length += 2 + len(self._lw_topic.encode("utf-8")) + 2 + len(self._lw_msg) var_header[6] |= 0x4 | (self._lw_qos & 0x1) << 3 | (self._lw_qos & 0x2) << 3 var_header[6] |= self._lw_retain << 5 @@ -914,10 +914,11 @@ def _send_str(self, string): :param str string: String to write to the socket. """ - self._sock.send(struct.pack("!H", len(string.encode("utf-8")))) if isinstance(string, str): + self._sock.send(struct.pack("!H", len(string.encode("utf-8")))) self._sock.send(str.encode(string, "utf-8")) else: + self._sock.send(struct.pack("!H", len(string))) self._sock.send(string) @staticmethod