from thrift.protocol import TCompactProtocol from thrift.transport import TSocket from thrift.transport import TTransport # create a TTransport object with a TSocket transport = TSocket.TSocket('localhost', 9090) transport = TTransport.TBufferedTransport(transport) # create a TCompactProtocol object with the TTransport protocol = TCompactProtocol.TCompactProtocol(transport) # serialize data into the protocol protocol.writeBool(True) protocol.writeI16(42) protocol.writeDouble(3.14) # send the serialized data over the transport transport.flush()
from thrift.protocol import TCompactProtocol from thrift.transport import TSocket from thrift.transport import TTransport # create a TTransport object with a TSocket transport = TSocket.TSocket('localhost', 9090) transport = TTransport.TBufferedTransport(transport) # create a TCompactProtocol object with the TTransport protocol = TCompactProtocol.TCompactProtocol(transport) # receive serialized data over the transport transport.open() is_admin = protocol.readBool() user_id = protocol.readI32() username = protocol.readString() # print out the deserialized data print("Is admin: {}".format(is_admin)) print("User ID: {}".format(user_id)) print("Username: {}".format(username))In this example, we create a TTransport object using a TSocket to connect to a remote server. We then create a TCompactProtocol object with the TTransport object, and use it to receive serialized data over the network. We assume that the data contains a boolean, an integer, and a string, which we then deserialize and print out to the console. Overall, the python thrift.protocol TCompactProtocol is a powerful tool for efficiently storing and transferring data over the network. It is part of the Apache Thrift package library, which provides a comprehensive set of tools for building scalable and cross-language services.