mqtt/pdu.py
def encode(self):
        '''
        Encode and store a CONNECT control packet. 
        @raise e: C{ValueError} if any encoded topic string exceeds 65535 bytes.
        @raise e: C{ValueError} if encoded username string exceeds 65535 bytes.
        '''
        header    = bytearray(1)
        varHeader = bytearray()
        payload   = bytearray()
        header[0] = 0x10            # packet code
        # ---- Variable header encoding section -----
        
        varHeader.extend(encodeString(self.version['tag'])) 
        varHeader.append(self.version['level']) # protocol Level
        flags =  (self.cleanStart << 1)
        if  self.willTopic is not None and self.willMessage is not None:
            flags |= 0x04 | (self.willRetain << 5) | (self.willQoS << 3)
        if self.username is not None:
            flags |= 0x80
        if self.password is not None:
            flags |= 0x40
        varHeader.append(flags)
        varHeader.extend(encode16Int(self.keepalive))
        # ------ Payload encoding section ----
        payload.extend(encodeString(self.clientId))
        if self.willTopic is not None and self.willMessage is not None:
            payload.extend(encodeString(self.willTopic))
            payload.extend(encodeString(self.willMessage))
        if self.username is not None:
             payload.extend(encodeString(self.username))
        if self.password is not None:
            payload.extend(encode16Int(len(self.password)))
            payload.extend(bytearray(self.password, encoding='ascii', errors='ignore'))
        # ---- Build the packet once all lengths are known ----
        header.extend(encodeLength(len(varHeader) + len(payload)))
        header.extend(varHeader)
        header.extend(payload)
        self.encoded = header
        return str(header) if PY2 else bytes(header)
def encode(self):
        '''
        Encode and store a CONNECT control packet. 
        @raise e: C{ValueError} if any encoded topic string exceeds 65535 bytes.
        @raise e: C{ValueError} if encoded username string exceeds 65535 bytes.
        '''
        header    = bytearray(1)
        varHeader = bytearray()
        payload   = bytearray()
        header[0] = 0x10            # packet code
        # ---- Variable header encoding section -----
        
        varHeader.extend(encodeString(self.version['tag'])) 
        varHeader.append(self.version['level']) # protocol Level
        flags =  (self.cleanStart << 1)
        if  self.willTopic is not None and self.willMessage is not None:
            flags |= 0x04 | (self.willRetain << 5) | (self.willQoS << 3)
        if self.username is not None:
            flags |= 0x80
        if self.password is not None:
            flags |= 0x40
        varHeader.append(flags)
        varHeader.extend(encode16Int(self.keepalive))
        # ------ Payload encoding section ----
        payload.extend(encodeString(self.clientId))
        if self.willTopic is not None and self.willMessage is not None:
            payload.extend(encodeString(self.willTopic))
            payload.extend(encodeString(self.willMessage))
        if self.username is not None:
             payload.extend(encodeString(self.username))
        if self.password is not None:
            payload.extend(encode16Int(len(self.password)))
            payload.extend(bytearray(self.password, encoding='ascii', errors='ignore'))
        # ---- Build the packet once all lengths are known ----
        header.extend(encodeLength(len(varHeader) + len(payload)))
        header.extend(varHeader)
        header.extend(payload)
        self.encoded = header
        return str(header) if PY2 else bytes(header)
Encode and store a CONNECT control packet. 
        @raise e: C{ValueError} if any encoded topic string exceeds 65535 bytes.
        @raise e: C{ValueError} if encoded username string exceeds 65535 bytes.