thrift的简单使用(三)

thrift的简单使用(三)


client


    
    from _thrift.product_server import ProductServer
    
    from _thrift import common
    from _thrift import product_type
    
    
    from thrift.transport import TSocket
    from thrift.transport import TTransport
    from thrift.protocol import TBinaryProtocol
    from thrift.server import TServer
    
    tsocket = TSocket.TSocket("127.0.0.1",9090)
    transportFactory = TTransport.TFramedTransportFactory()
    transport = transportFactory.getTransport(tsocket)
    protocol = TBinaryProtocol.TBinaryProtocol(transport)
    client = ProductServer.Client(protocol)
    transport.open()
    
    reqHeader = common.ttypes.RequestHeader(requester='hello',search_id=9)
    condition = product_type.ttypes.ProductQueryCondition()
    condition.collection = 'table1'
    condition.condition_map = {"key":
                               product_type.ttypes.VariableValue(
                                   product_type.ttypes.ParamTypeCode.STRING,
                                   'h'
                               )
                               }
    condition.offset = 10
    condition.limit = 5
    condition.displayed_field = ['key','name']
    condition.sorted_field = {"keyword":12}
    ret = client.get_product(reqHeader,condition)
    
    print(ret)


server

    
    import time
    
    from _thrift.product_server import ProductServer
    from _thrift import product_type, common
    
    from thrift.transport  import TSocket
    from thrift.transport import TTransport
    from thrift.protocol import TBinaryProtocol
    from thrift.server import TServer
    
    
    
    class  ProductHandler:
    
        def get_product(self, header, condition):
            ret = product_type.ttypes.ProductQueryResult()
            # product_info = product_type.ttypes.ProductInfo()
            result_dic = {
                'table1': [product_type.ttypes.ProductInfo(id=str(1000 + n),
                                                           xxx_id=2000 + n,
                                                           name='name%s' % n,
                                                           tags=['tag1', 'tag2'],
                                                           description='description%s' % n,
                                                           source_url='source_url%s' % n,
                                                           price=n * 2,
                                                           xxx=n * 3,
                                                           medias=[product_type.ttypes.ProductMedia(alt='media1',
                                                                                                   type=product_type.ttypes.MediaTypeCode.IMAGE,
                                                                                                   media_uri='media_uri',
                                                                                                   width=280,
                                                                                                   height=200),],
                                                           attr={"key": product_type.ttypes.VariableValue(
                                                               product_type.ttypes.ParamTypeCode.STRING, "value")},
                                                           create_time= 102244,
                                                           update_time= 102212
                                                           ) for n in range(5)],
                'table2': [product_type.ttypes.ProductInfo(id=str(1000 + 2 * n),
                                                           xxx_id=2000 + 2 * n,
                                                           name='name%s' % n,
                                                           tags=['tag1', 'tag2'],
                                                           description='description%s' % n,
                                                           xxx='source_url%s' % n,
                                                           price=n * 2,
                                                           xxx=n * 3,
                                                           medias=[product_type.ttypes.ProductMedia(alt='media1',
                                                                                                   type=product_type.ttypes.MediaTypeCode.IMAGE,
                                                                                                   media_uri='media_uri',
                                                                                                   width=280,
                                                                                                   height=200),],
                                                           attr={"key": product_type.ttypes.VariableValue(
                                                               product_type.ttypes.ParamTypeCode.STRING, "value")},
                                                           create_time=1121212,
                                                           update_time=21321321
                                                           ) for n in range(5)],
            }
    
            ret.product_list = result_dic.get(condition.collection, None)
            ret.result_count = len(header.requester) + 1
            ret.response_length = len(header.requester) + 1
            return ret
    
    
 
    
    handler = ProductHandler()
    processor = ProductServer.Processor(handler)
    transport = TSocket.TServerSocket("127.0.0.1",9090)
    tfactory = TTransport.TFramedTransportFactory()
    pfactory = TBinaryProtocol.TBinaryProtocolFactory()
    
    server = TServer.TSimpleServer(processor,transport,tfactory,pfactory)
    print("starting  thrift  server in python")
    server.serve()
    
    
    print("启动!")




Buy me a 肥仔水!