@@ -0,0 +1,1284 @@
+%0Aimport struct%0A%0Adef send_tlvdata(handle, data):%0A    if not isintance(type, int):%0A        raise Exception()%0A    if isinstance(data, str):%0A        tl = len(data)%0A        if tl %3C 16777216:%0A            #type for string is '0', so we don't need%0A            #to xor anything in%0A            handle.write(struct.pack(%22!I%22, tl))%0A        else:%0A            raise Exception(%22String data length exceeds protocol%22)%0A        handle.write(data)%0A        handle.flush()%0A    elif isinstance(data, dict):  # JSON currently only goes to 4 bytes%0A        sdata = json.dumps(data, separators=(',',':'))%0A        tl = len(sdata)%0A        if tl %3E 16777215:%0A            raise Exception(%22JSON data exceeds protocol limits%22)%0A        # xor in the type (0b1 %3C%3C 24)%0A        tl %7C= 16777216%0A        handle.write(struct.pack(%22!I%22, tl))%0A        handle.write(sdata)%0A        handle.flush()%0A%0Adef recv_tlvdata(handle):%0A    tl = handle.read(4)%0A    tl = struct.unpack(%22!B%22, tl)%5B0%5D%0A    if tl & 0b10000000:%0A        raise Exception(%22Protocol Violation, reserved bit set%22)%0A    # 4 byte tlv%0A    dlen = tl & 16777215  # 24 ones%0A    type = (tl & 2130706432) %3E%3E 24  # 7 ones, followed by 24 zeroes%0A    if type == 0:%0A        return(handle.read(dlen))%0A    elif type == 1:%0A        sdata = handle.read(dlen)%0A        return json.loads(sdata)%0A

Add a tlvdata module