def subscribe(self, topic, qos=0):
    assert (self.cb is not None), 'Subscribe callback is not set'
    pkt = bytearray(b'\x82\x00\x00\x00')
    self.pid += 1
    struct.pack_into('!BH', pkt, 1, (((2 + 2) + len(topic)) + 1), self.pid)
    self.sock.write(pkt)
    self._send_str(topic)
    self.sock.write(qos.to_bytes(1, 'little'))
    while 1:
        op = self.wait_msg()
        if (op == 144):
            resp = self.sock.read(4)
            assert ((resp[1] == pkt[2]) and (resp[2] == pkt[3]))
            if (resp[3] == 128):
                raise MQTTException(resp[3])
            return
def subscribe(self, topic, qos=0):
    assert (self.cb is not None), 'Subscribe callback is not set'
    pkt = bytearray(b'\x82\x00\x00\x00')
    self.pid += 1
    struct.pack_into('!BH', pkt, 1, (((2 + 2) + len(topic)) + 1), self.pid)
    self.sock.write(pkt)
    self._send_str(topic)
    self.sock.write(qos.to_bytes(1, 'little'))
    while 1:
        op = self.wait_msg()
        if (op == 144):
            resp = self.sock.read(4)
            assert ((resp[1] == pkt[2]) and (resp[2] == pkt[3]))
            if (resp[3] == 128):
                raise MQTTException(resp[3])
            return