雷顿大数据技术学院:http://www.leidun.site/


1 编写 thrift文件

文件名

helloworld.thrift

HelloWorld{ping()say(:msg)}


使用命令 thrift --gen py hellowrold.thrift生成基础文件

生成结果目录格式如下


server.py


syssys.path.insert()sys.pathhelloworldHelloWorldhelloworld.ttypes*thrift.transportTSocketthrift.transportTTransportthrift.protocolTBinaryProtocolthrift.serverTServerHelloWorldHandler:():(msg):ret=+msgretrethandler=HelloWorldHandler()processor=HelloWorld.Processor(handler)transport=TSocket.TServerSocket()tfactory=TTransport.TBufferedTransportFactory()pfactory=TBinaryProtocol.TBinaryProtocolFactory()server=TServer.TSimpleServer(processortransporttfactorypfactory)server.serve()



client.py


syssys.path.insert()helloworldHelloWorldthriftThriftthrift.transportTSocketthrift.transportTTransportthrift.protocolTBinaryProtocol:transport=TSocket.TSocket()transport=TTransport.TBufferedTransport(transport)protocol=TBinaryProtocol.TBinaryProtocol(transport)client=HelloWorld.Client(protocol)transport.open()msg=client.say()+msgtransport.close()Thrift.TExceptionex:%(ex.message)


启动server clien

python server.py

python client.py