Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
remaining_length = 2
if not isinstance(topic, (list, tuple)):
topics = [topic]
else:
topics = topic
for t in topics:
remaining_length += 2 + len(t)
properties = cls._build_properties_data(kwargs, protocol.proto_ver)
remaining_length += len(properties)
command = MQTTCommands.UNSUBSCRIBE | 0x2
packet = bytearray()
packet.append(command)
packet.extend(pack_variable_byte_integer(remaining_length))
local_mid = cls.id_generator.next_id()
packet.extend(struct.pack("!H", local_mid))
packet.extend(properties)
for t in topics:
cls._pack_str16(packet, t)
logger.info('[SEND UNSUB] %s', topics)
return local_mid, packet
if username is not None:
remaining_length += 2 + len(username)
connect_flags |= 0x80
if password is not None:
connect_flags |= 0x40
remaining_length += 2 + len(password)
command = MQTTCommands.CONNECT
packet = bytearray()
packet.append(command)
prop_bytes = cls._build_properties_data(kwargs, protocol.proto_ver)
remaining_length += len(prop_bytes)
packet.extend(pack_variable_byte_integer(remaining_length))
packet.extend(struct.pack("!H" + str(len(protocol.proto_name)) + "sBBH",
len(protocol.proto_name),
protocol.proto_name,
protocol.proto_ver,
connect_flags,
keepalive))
packet.extend(prop_bytes)
cls._pack_str16(packet, client_id)
if will_message:
packet += will_prop_bytes
cls._pack_str16(packet, will_message.topic)
cls._pack_str16(packet, will_message.payload)
def _build_properties_data(cls, properties_dict, protocol_version):
if protocol_version < MQTTv50:
return bytearray()
data = bytearray()
for property_name, property_value in properties_dict.items():
property = Property.factory(name=property_name)
if property is None:
logger.warning('[GMQTT] property {} is not supported, it was ignored'.format(property_name))
continue
property_bytes = property.dumps(property_value)
data.extend(property_bytes)
result = pack_variable_byte_integer(len(data))
result.extend(data)
return result
return packet
elif self.bytes_struct == 'u8x2':
if isinstance(data[0], str):
self._dump_user_property(data, packet)
else:
for kv_pair in data:
self._dump_user_property(kv_pair, packet)
return packet
elif self.bytes_struct == 'b':
packet.extend(struct.pack('!B', self.id))
packet.extend(struct.pack('!H', len(data)))
packet.extend(data)
return packet
elif self.bytes_struct == 'vbi':
packet.extend(struct.pack('!B', self.id))
packet.extend(pack_variable_byte_integer(data))
return packet
packet.extend(struct.pack('!B', self.id))
packet.extend(struct.pack(self.bytes_struct, data))
return packet
connect_flags = 0
clean_session = True
if clean_session:
connect_flags |= 0x02
connect_flags |= 0x80 # username
connect_flags |= 0x40 # password
command = MQTTCommands.CONNECT
packet = bytearray()
packet.append(command)
remaining_length += len(prop_bytes)
packet.extend(pack_variable_byte_integer(remaining_length))
packet.extend(struct.pack("!H" + str(len(protocol.proto_name)) + "sBBH",
len(protocol.proto_name),
protocol.proto_name,
protocol.proto_ver,
connect_flags,
keepalive))
packet.extend(prop_bytes)
return packet