Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _send_pubcomp(self, mid, dup, reason_code=0):
self._send_command_with_mid(MQTTCommands.PUBCOMP, mid, dup, reason_code=reason_code)
def _send_puback(self, mid, reason_code=0):
self._send_command_with_mid(MQTTCommands.PUBACK, mid, False, reason_code=reason_code)
def _send_pubrel(self, mid, dup, reason_code=0):
self._send_command_with_mid(MQTTCommands.PUBREL | 2, mid, dup, reason_code=reason_code)
def build_package(cls, subscription, protocol, **kwargs) -> Tuple[int, bytes]:
remaining_length = 2
if not isinstance(subscription, (list, tuple)):
subscriptions = [subscription]
else:
subscriptions = subscription
topics = []
for s in subscriptions:
remaining_length += 2 + len(s.topic) + 1
topics.append(s.topic)
properties = cls._build_properties_data(kwargs, protocol.proto_ver)
remaining_length += len(properties)
command = MQTTCommands.SUBSCRIBE | (False << 3) | 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 s in subscriptions:
cls._pack_str16(packet, s.topic)
subscribe_options = s.retain_handling_options << 4 | s.retain_as_published << 3 | s.no_local << 2 | s.qos
packet.append(subscribe_options)
logger.info('[SEND SUB] %s %s', local_mid, topics)
return local_mid, packet
def connection_lost(self, exc):
super(MQTTProtocol, self).connection_lost(exc)
self._connection.put_package((MQTTCommands.DISCONNECT, b''))
if self._read_loop_future is not None:
self._read_loop_future.cancel()
self._read_loop_future = None
self._queue = asyncio.Queue()
def build_package(cls, topic, protocol, **kwargs) -> Tuple[int, bytes]:
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
def __get_handler__(self, cmd):
cmd_type = cmd & 0xF0
if cmd_type not in self._handler_cache:
handler_name = '_handle_{}_packet'.format(MQTTCommands(cmd_type).name.lower())
self._handler_cache[cmd_type] = getattr(self, handler_name, self._default_handler)
return self._handler_cache[cmd_type]
if clean_session:
connect_flags |= 0x02
if will_message:
will_prop_bytes = cls._build_properties_data(will_message.properties, protocol.proto_ver)
remaining_length += 2 + len(will_message.topic) + 2 + len(will_message.payload) + len(will_prop_bytes)
connect_flags |= 0x04 | ((will_message.qos & 0x03) << 3) | ((will_message.retain & 0x01) << 5)
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)
def build_package(cls, message, protocol) -> Tuple[int, bytes]:
command = MQTTCommands.PUBLISH | ((message.dup & 0x1) << 3) | (message.qos << 1) | (message.retain & 0x1)
packet = bytearray()
packet.append(command)
remaining_length = 2 + len(message.topic) + message.payload_size
prop_bytes = cls._build_properties_data(message.properties, protocol_version=protocol.proto_ver)
remaining_length += len(prop_bytes)
if message.payload_size == 0:
logger.debug("Sending PUBLISH (q%d), '%s' (NULL payload)", message.qos, message.topic)
else:
logger.debug("Sending PUBLISH (q%d), '%s', ... (%d bytes)", message.qos, message.topic, message.payload_size)
if message.qos > 0:
# For message id
remaining_length += 2
def send_ping_request(self):
self.send_simple_command_packet(MQTTCommands.PINGREQ)