【Thrift】python 例子

来源:互联网 发布:好压mac 编辑:程序博客网 时间:2024/06/05 23:06
service MyService{  string get()}
import socketimport syssys.path.append('./gen-py')from helloworld import MyServicefrom helloworld.ttypes import *from thrift.transport import TSocketfrom thrift.transport import TTransportfrom thrift.protocol import TBinaryProtocolfrom thrift.server import TServerclass HelloWorldHandler:  def get(self):    return "glgl"handler = HelloWorldHandler()processor = MyService.Processor(handler)transport = TSocket.TServerSocket("localhost", 9090)tfactory = TTransport.TBufferedTransportFactory()pfactory = TBinaryProtocol.TBinaryProtocolFactory()server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)print("Starting thrift server in python...")server.serve()print("done!")

import syssys.path.append('./gen-py')from helloworld import MyServicefrom thrift import Thriftfrom thrift.transport import TSocketfrom thrift.transport import TTransportfrom thrift.protocol import TBinaryProtocoltry:  transport = TSocket.TSocket('localhost', 9090)  transport = TTransport.TBufferedTransport(transport)  protocol = TBinaryProtocol.TBinaryProtocol(transport)  client = MyService.Client(protocol)  transport.open()  print("client - get")  print("server - " + client.get())  transport.close()except Thrift.TException:  print()


原创粉丝点击