import socket import sys from hello import HelloService from hello.ttypes import * from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol from thrift.server import TServer class HelloServiceHandler: def test(self, msg): ret = "TReceived: " + msg print ret return ret handler = HelloServiceHandler() processor = HelloService.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 sys from hello import HelloService from thrift import Thrift from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol try: transport = TSocket.TSocket('localhost', 9090) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = HelloService.Client(protocol) transport.open() print "client - say" msg = client.test("Hello!(test)") print "server - " + msg transport.close() except Thrift.TException, ex: print "%s" % (ex.message)
#!/usr/bin/env python import sys sys.path.append('gen-py') import hello from hello import HelloService from hello.ttypes import HelloMsg from thrift.transport import TTransport from thrift.transport import TSocket from thrift.protocol import TBinaryProtocol host, port = 'localhost', 5555 text = (' '.join(sys.argv[1:])).strip() if not text: text = 'from Python' transport = TTransport.TFramedTransport(TSocket.TSocket(host, port)) client = HelloService.Client(TBinaryProtocol.TBinaryProtocol(transport)) transport.open() try: client.ping() msg = client.sayHello(HelloMsg(text)) print(msg) finally: transport.close()