Ejemplo n.º 1
0
def thrift_obj_in_bytes(thrift_obj): 
    """
    Encodes a Thrift object using TBinaryProtocol.

    :param thrift_obj
    :returns: TBinaryProtocol bytes represenation of thrift_obj.
    """
    trans = TMemoryBuffer()
    thrift_obj.write(TBinaryProtocol(trans))
    return trans.getvalue();
    return bytes(trans.getvalue())
def thrift_obj_in_bytes(thrift_obj):
    """
    Encodes a Thrift object using TBinaryProtocol.

    :param thrift_obj
    :returns: TBinaryProtocol bytes represenation of thrift_obj.
    """
    trans = TMemoryBuffer()
    thrift_obj.write(TBinaryProtocol(trans))
    return trans.getvalue()
    return bytes(trans.getvalue())
Ejemplo n.º 3
0
def test_write_string():
    b = TMemoryBuffer()
    b = TCyBufferedTransport(b)
    proto.write_val(b, TType.STRING, "hello world!")
    b.flush()
    assert "00 00 00 0c 68 65 6c 6c 6f 20 77 6f 72 6c 64 21" == \
        hexlify(b.getvalue())

    b = TMemoryBuffer()
    b = TCyBufferedTransport(b)
    proto.write_val(b, TType.STRING, u("你好世界"))
    b.flush()
    assert "00 00 00 0c e4 bd a0 e5 a5 bd e4 b8 96 e7 95 8c" == \
        hexlify(b.getvalue())
Ejemplo n.º 4
0
def test_pack_string():
    b = TMemoryBuffer()
    p = proto.TCyBinaryProtocol(b)
    p.write_val(TType.STRING, "hello world!")
    p.write_message_end()
    assert_equal("00 00 00 0c 68 65 6c 6c 6f 20 77 6f 72 6c 64 21",
                 hexlify(b.getvalue()))

    b = TMemoryBuffer()
    p = proto.TCyBinaryProtocol(b)
    p.write_val(TType.STRING, u("你好世界"))
    p.write_message_end()
    assert_equal("00 00 00 0c e4 bd a0 e5 a5 bd e4 b8 96 e7 95 8c",
                 hexlify(b.getvalue()))
Ejemplo n.º 5
0
def test_write_string():
    b = TMemoryBuffer()
    b = TCyBufferedTransport(b)
    proto.write_val(b, TType.STRING, "hello world!")
    b.flush()
    assert "00 00 00 0c 68 65 6c 6c 6f 20 77 6f 72 6c 64 21" == \
        hexlify(b.getvalue())

    b = TMemoryBuffer()
    b = TCyBufferedTransport(b)
    proto.write_val(b, TType.STRING, u("你好世界"))
    b.flush()
    assert "00 00 00 0c e4 bd a0 e5 a5 bd e4 b8 96 e7 95 8c" == \
        hexlify(b.getvalue())
Ejemplo n.º 6
0
def test_pack_string():
    b = TMemoryBuffer()
    p = proto.TCyBinaryProtocol(b)
    p.write_val(TType.STRING, "hello world!")
    p.write_message_end()
    assert "00 00 00 0c 68 65 6c 6c 6f 20 77 6f 72 6c 64 21" == \
        hexlify(b.getvalue())

    b = TMemoryBuffer()
    p = proto.TCyBinaryProtocol(b)
    p.write_val(TType.STRING, u("你好世界"))
    p.write_message_end()
    assert "00 00 00 0c e4 bd a0 e5 a5 bd e4 b8 96 e7 95 8c" == \
        hexlify(b.getvalue())
Ejemplo n.º 7
0
def test_write_i32():
    b = TMemoryBuffer()
    b = TCyBufferedTransport(b)
    proto.write_val(b, TType.I32, 1234567890)
    b.flush()

    assert "49 96 02 d2" == hexlify(b.getvalue())
Ejemplo n.º 8
0
def test_write_i16():
    b = TMemoryBuffer()
    b = TCyBufferedTransport(b)
    proto.write_val(b, TType.I16, 12345)
    b.flush()

    assert "30 39" == hexlify(b.getvalue())
Ejemplo n.º 9
0
def test_write_i8():
    b = TMemoryBuffer()
    b = TCyBufferedTransport(b)
    proto.write_val(b, TType.I08, 123)
    b.flush()

    assert "7b" == hexlify(b.getvalue())
Ejemplo n.º 10
0
def test_write_bool():
    b = TMemoryBuffer()
    b = TCyBufferedTransport(b)
    proto.write_val(b, TType.BOOL, 1)
    b.flush()

    assert "01" == hexlify(b.getvalue())
Ejemplo n.º 11
0
def test_write_i8():
    b = TMemoryBuffer()
    b = TCyBufferedTransport(b)
    proto.write_val(b, TType.I08, 123)
    b.flush()

    assert "7b" == hexlify(b.getvalue())
Ejemplo n.º 12
0
def test_write_i32():
    b = TMemoryBuffer()
    b = TCyBufferedTransport(b)
    proto.write_val(b, TType.I32, 1234567890)
    b.flush()

    assert "49 96 02 d2" == hexlify(b.getvalue())
Ejemplo n.º 13
0
def test_write_i16():
    b = TMemoryBuffer()
    b = TCyBufferedTransport(b)
    proto.write_val(b, TType.I16, 12345)
    b.flush()

    assert "30 39" == hexlify(b.getvalue())
Ejemplo n.º 14
0
def test_write_message_begin():
    b = TMemoryBuffer()
    p = proto.TCyBinaryProtocol(b)
    p.write_message_begin("test", TType.STRING, 1)
    p.write_message_end()
    assert "80 01 00 0b 00 00 00 04 74 65 73 74 00 00 00 01" == \
        hexlify(b.getvalue())
Ejemplo n.º 15
0
def base64_thrift(thrift_obj):
    trans = TMemoryBuffer()
    tbp = TBinaryProtocol(trans)

    thrift_obj.write(tbp)

    return b64encode(bytes(trans.getvalue())).strip()
Ejemplo n.º 16
0
def test_write_empty_struct():
    b = TMemoryBuffer()
    item = TItem()
    p = proto.TCyBinaryProtocol(b)
    p.write_struct(item)
    p.write_message_end()
    assert "00" == hexlify(b.getvalue())
Ejemplo n.º 17
0
def test_write_bool():
    b = TMemoryBuffer()
    b = TCyBufferedTransport(b)
    proto.write_val(b, TType.BOOL, 1)
    b.flush()

    assert "01" == hexlify(b.getvalue())
Ejemplo n.º 18
0
def test_write_message_begin():
    b = TMemoryBuffer()
    p = proto.TCyBinaryProtocol(b)
    p.write_message_begin('test', TType.STRING, 1)
    p.write_message_end()
    assert_equal("80 01 00 0b 00 00 00 04 74 65 73 74 00 00 00 01",
                 hexlify(b.getvalue()))
Ejemplo n.º 19
0
def test_write_struct():
    b = TMemoryBuffer()
    item = TItem(id=123, phones=["123456", "abcdef"])
    p = proto.TCyBinaryProtocol(b)
    p.write_struct(item)
    p.write_message_end()
    assert ("08 00 01 00 00 00 7b 0f 00 02 0b 00 00 00 02 00 00 00 "
            "06 31 32 33 34 35 36 00 00 00 06 61 62 63 64 65 66 00") == \
        hexlify(b.getvalue())
Ejemplo n.º 20
0
def thrift_obj_in_bytes(thrift_obj):  # pragma: no cover
    """
    Returns TBinaryProtocol encoded Thrift object.

    :param thrift_obj: thrift object to encode
    :returns: thrift object in TBinaryProtocol format bytes.
    """
    trans = TMemoryBuffer()
    thrift_obj.write(TBinaryProtocol(trans))
    return bytes(trans.getvalue())
Ejemplo n.º 21
0
def thrift_obj_in_bytes(thrift_obj):  # pragma: no cover
    """
    Returns TBinaryProtocol encoded Thrift object.

    :param thrift_obj: thrift object to encode
    :returns: thrift object in TBinaryProtocol format bytes.
    """
    trans = TMemoryBuffer()
    thrift_obj.write(TBinaryProtocol(trans))

    return bytes(trans.getvalue())
Ejemplo n.º 22
0
    def message_received(self, frame):
        output_buffer = TMemoryBuffer()

        yield from self._processor.process(
            iprot=self._protocol_factory.get_protocol(TMemoryBuffer(frame)),
            oprot=self._protocol_factory.get_protocol(output_buffer),
        )

        output_frame = output_buffer.getvalue()
        output_length = struct.pack('!i', len(output_frame))

        self._transport.write(output_length + output_frame)
Ejemplo n.º 23
0
def span_to_bytes(thrift_span):
    """
    Returns a TBinaryProtocol encoded Thrift span.

    :param thrift_span: thrift object to encode.
    :returns: thrift object in TBinaryProtocol format bytes.
    """
    transport = TMemoryBuffer()
    protocol = TBinaryProtocol(transport)
    thrift_span.write(protocol)

    return bytes(transport.getvalue())
Ejemplo n.º 24
0
def encode_bytes_list(binary_thrift_obj_list):  # pragma: no cover
    """
    Returns a TBinaryProtocol encoded list of Thrift objects.

    :param binary_thrift_obj_list: list of TBinaryProtocol objects to encode.
    :returns: bynary object representing the encoded list.
    """
    transport = TMemoryBuffer()
    write_list_begin(transport, TType.STRUCT, len(binary_thrift_obj_list))
    for thrift_bin in binary_thrift_obj_list:
        transport.write(thrift_bin)

    return bytes(transport.getvalue())
Ejemplo n.º 25
0
def thrift_objs_in_bytes(thrift_obj_list):  # pragma: no cover
    """
    Returns TBinaryProtocol encoded Thrift objects.

    :param thrift_obj_list: thrift objects list to encode
    :returns: thrift objects in TBinaryProtocol format bytes.
    """
    transport = TMemoryBuffer()
    protocol = TBinaryProtocol(transport)
    write_list_begin(transport, TType.STRUCT, len(thrift_obj_list))
    for thrift_obj in thrift_obj_list:
        thrift_obj.write(protocol)

    return bytes(transport.getvalue())
Ejemplo n.º 26
0
def test_json_proto_api_write():
    obj = TItem(id=13, phones=["5234", "12346456"])
    trans = TMemoryBuffer()

    p = TJSONProtocol(trans)
    p.write_struct(obj)

    data = trans.getvalue().decode("utf-8")
    length = data[0:4]

    import json
    data = json.loads(data[4:])

    assert length == "\x00\x00\x00S" and data == {
        "metadata": {"version": 1},
        "payload": {"phones": ["5234", "12346456"], "id": 13}}
Ejemplo n.º 27
0
def test_json_proto_api_write():
    obj = TItem(id=13, phones=["5234", "12346456"])
    trans = TMemoryBuffer()

    p = TJSONProtocol(trans)
    p.write_struct(obj)

    data = trans.getvalue().decode("utf-8")
    length = data[0:4]

    import json
    data = json.loads(data[4:])

    assert length == "\x00\x00\x00S" and data == {
        "metadata": {"version": 1},
        "payload": {"phones": ["5234", "12346456"], "id": 13}}
Ejemplo n.º 28
0
 def write_it(stream):
     transport_out = TMemoryBuffer()
     protocol_out = TBinaryProtocol(transport_out)
     cls._topology.write(protocol_out)
     transport_bytes = transport_out.getvalue()
     stream.write(transport_bytes)
Ejemplo n.º 29
0
 def write_it(stream):
     transport_out = TMemoryBuffer()
     protocol_out = TBinaryProtocol(transport_out)
     cls.thrift_topology.write(protocol_out)
     transport_bytes = transport_out.getvalue()
     stream.write(transport_bytes)
Ejemplo n.º 30
0
    def build_package(cls, fbns_auth: FBNSAuth, clean_session, keepalive, protocol, will_message=None, **kwargs):
        keepalive = 900

        connect_payload = thrift.Connect()
        connect_payload.clientIdentifier = fbns_auth.clientId

        client_info = thrift.ClientInfo()
        client_info.userId = fbns_auth.userId
        client_info.userAgent = '[FBAN/MQTT;FBAV/64.0.0.14.96;FBBV/125398467;FBDM/{density=4.0,width=1440,height=2392};FBLC/en_US;FBCR/;FBMF/LGE;FBBD/lge;FBPN/com.instagram.android;FBDV/RS988;FBSV/6.0.1;FBLR/0;FBBK/1;FBCA/armeabi-v7a:armeabi;]'
        client_info.clientCapabilities = 439
        client_info.endpointCapabilities = 128
        client_info.publishFormat = 1
        client_info.noAutomaticForeground = True
        client_info.makeUserAvailableInForeground = False
        client_info.deviceId = fbns_auth.deviceId
        client_info.isInitiallyForeground = False
        client_info.networkType = 1
        client_info.networkSubtype = 0

        last_monday = datetime.now() - relativedelta(weekday=calendar.MONDAY, hour=0, minute=0, second=0, microsecond=0)
        last_monday = last_monday.timestamp()
        session_id = int((time.time() - last_monday) * 1000)

        client_info.clientMqttSessionId = session_id
        client_info.subscribeTopics = [int(FBNSMQTTClient.MESSAGE_TOPIC_ID), int(FBNSMQTTClient.REG_RESP_TOPIC_ID)]
        client_info.clientType = 'device_auth'
        client_info.appId = 567310203415052  # Const
        client_info.deviceSecret = fbns_auth.deviceSecret
        client_info.clientStack = 3

        connect_payload.clientInfo = client_info
        connect_payload.password = fbns_auth.password

        trans = TMemoryBuffer()
        p = TCompactProtocol(trans)
        p.write_struct(connect_payload)

        data = trans.getvalue()
        prop_bytes = zlib.compress(data, level=9)

        remaining_length = 2 + len(protocol.proto_name) + 1 + 1 + 2

        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
Ejemplo n.º 31
0
def test_pack_i8():
    b = TMemoryBuffer()
    p = proto.TCyBinaryProtocol(b)
    p.write_val(TType.I08, 123)
    p.write_message_end()
    assert "7b" == hexlify(b.getvalue())
Ejemplo n.º 32
0
def _build_log_message(span):
    trans = TMemoryBuffer()
    protocol = TBinaryProtocol(trans=trans)
    span.write(protocol)
    return base64.b64encode(trans.getvalue())
Ejemplo n.º 33
0
def test_write_double():
    b = TMemoryBuffer()
    b = TCyBufferedTransport(b)
    proto.write_val(b, TType.DOUBLE, 1234567890.1234567890)
    b.flush()
    assert "41 d2 65 80 b4 87 e6 b7" == hexlify(b.getvalue())
Ejemplo n.º 34
0
def test_pack_i16():
    b = TMemoryBuffer()
    p = proto.TCyBinaryProtocol(b)
    p.write_val(TType.I16, 12345)
    p.write_message_end()
    assert "30 39" == hexlify(b.getvalue())
Ejemplo n.º 35
0
def test_pack_double():
    b = TMemoryBuffer()
    p = proto.TCyBinaryProtocol(b)
    p.write_val(TType.DOUBLE, 1234567890.1234567890)
    p.write_message_end()
    assert_equal("41 d2 65 80 b4 87 e6 b7", hexlify(b.getvalue()))
Ejemplo n.º 36
0
def test_pack_i64():
    b = TMemoryBuffer()
    p = proto.TCyBinaryProtocol(b)
    p.write_val(TType.I64, 1234567890123456789)
    p.write_message_end()
    assert_equal("11 22 10 f4 7d e9 81 15", hexlify(b.getvalue()))
Ejemplo n.º 37
0
def test_write_i64():
    b = TMemoryBuffer()
    b = TCyBufferedTransport(b)
    proto.write_val(b, TType.I64, 1234567890123456789)
    b.flush()
    assert "11 22 10 f4 7d e9 81 15" == hexlify(b.getvalue())
Ejemplo n.º 38
0
def test_write_i64():
    b = TMemoryBuffer()
    b = TCyBufferedTransport(b)
    proto.write_val(b, TType.I64, 1234567890123456789)
    b.flush()
    assert "11 22 10 f4 7d e9 81 15" == hexlify(b.getvalue())
Ejemplo n.º 39
0
def test_write_double():
    b = TMemoryBuffer()
    b = TCyBufferedTransport(b)
    proto.write_val(b, TType.DOUBLE, 1234567890.1234567890)
    b.flush()
    assert "41 d2 65 80 b4 87 e6 b7" == hexlify(b.getvalue())
Ejemplo n.º 40
0
def test_pack_i16():
    b = TMemoryBuffer()
    p = proto.TCyBinaryProtocol(b)
    p.write_val(TType.I16, 12345)
    p.write_message_end()
    assert_equal("30 39", hexlify(b.getvalue()))
Ejemplo n.º 41
0
def test_pack_i32():
    b = TMemoryBuffer()
    p = proto.TCyBinaryProtocol(b)
    p.write_val(TType.I32, 1234567890)
    p.write_message_end()
    assert_equal("49 96 02 d2", hexlify(b.getvalue()))
Ejemplo n.º 42
0
def test_pack_i32():
    b = TMemoryBuffer()
    p = proto.TCyBinaryProtocol(b)
    p.write_val(TType.I32, 1234567890)
    p.write_message_end()
    assert "49 96 02 d2" == hexlify(b.getvalue())
def serialize(thrift_object,
              protocol_factory=TBinaryProtocolFactory()):
    transport = TMemoryBuffer()
    protocol = protocol_factory.get_protocol(transport)
    thrift_object.write(protocol)
    return transport.getvalue()
Ejemplo n.º 44
0
def test_pack_i64():
    b = TMemoryBuffer()
    p = proto.TCyBinaryProtocol(b)
    p.write_val(TType.I64, 1234567890123456789)
    p.write_message_end()
    assert "11 22 10 f4 7d e9 81 15" == hexlify(b.getvalue())
Ejemplo n.º 45
0
def test_pack_double():
    b = TMemoryBuffer()
    p = proto.TCyBinaryProtocol(b)
    p.write_val(TType.DOUBLE, 1234567890.1234567890)
    p.write_message_end()
    assert "41 d2 65 80 b4 87 e6 b7" == hexlify(b.getvalue())
Ejemplo n.º 46
0
def serialize(thrift_object, protocol_factory=TBinaryProtocolFactory()):
    transport = TMemoryBuffer()
    protocol = protocol_factory.get_protocol(transport)
    thrift_object.write(protocol)
    return transport.getvalue()
Ejemplo n.º 47
0
def _build_log_message(span):
    trans = TMemoryBuffer()
    protocol = TBinaryProtocol(trans=trans)
    span.write(protocol)
    return base64.b64encode(trans.getvalue())