def to_bytes(self, fixed_header: MQTTFixedHeader, variable_header: MQTTVariableHeader): out = b'' for topic in self.topics: out += encode_string(topic[0]) out += int_to_bytes(topic[1], 1) return out
def to_bytes(self, fixed_header: MQTTFixedHeader, variable_header: ConnectVariableHeader): out = bytearray() # Client identifier out.extend(encode_string(self.client_id)) # Will topic / message if variable_header.will_flag: out.extend(encode_string(self.will_topic)) out.extend(encode_data_with_length(self.will_message)) # username if variable_header.username_flag: out.extend(encode_string(self.username)) # password if variable_header.password_flag: out.extend(encode_string(self.password)) return out
def to_bytes(self): out = bytearray() # Protocol name out.extend(encode_string(self.proto_name)) # Protocol level out.append(self.proto_level) # flags out.append(self.flags) # keep alive out.extend(int_to_bytes(self.keep_alive, 2)) return out
def to_bytes(self, fixed_header: MQTTFixedHeader, variable_header: MQTTVariableHeader): out = b"" for topic in self.topics: out += encode_string(topic) return out
def test_encode_string(self): encoded = encode_string("AA") self.assertEqual(b"\x00\x02AA", encoded)
def to_bytes(self): out = bytearray() out.extend(encode_string(self.topic_name)) if self.packet_id is not None: out.extend(int_to_bytes(self.packet_id, 2)) return out
def test_encode_string(self): encoded = encode_string('AA') self.assertEqual(b'\x00\x02AA', encoded)