CentOS thrift python demo
生活随笔
收集整理的這篇文章主要介紹了
CentOS thrift python demo
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
編輯接口文件 hellowworld.thrift
service HelloWorld {string ping(),string say(1:string msg) }
編輯 server.py
#!/usr/bin/env pythonimport socket import sys sys.path.append('./gen-py')from helloworld import HelloWorld from helloworld.ttypes import *from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol from thrift.server import TServerclass HelloWorldHandler:def ping(self):return "pong"def say(self, msg):ret = "Received: " + msgprint retreturn rethandler = HelloWorldHandler() processor = HelloWorld.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!"
編輯 client.py
#!/usr/bin/env pythonimport sys sys.path.append('./gen-py')from helloworld import HelloWorldfrom thrift import Thrift from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TBinaryProtocoltry:transport = TSocket.TSocket('localhost', 9090)transport = TTransport.TBufferedTransport(transport)protocol = TBinaryProtocol.TBinaryProtocol(transport)client = HelloWorld.Client(protocol)transport.open()print "client - ping"print "server - " + client.ping()print "client - say"msg = client.say("Hello!")print "server - " + msgtransport.close()except Thrift.TException, ex:print "%s" % (ex.message)
運行:
thrift --gen py helloworld.thrift python server.py python client.py #這個分一個窗口運行
如果修改里面的一個方法或者增加一個調(diào)用方法的話,需要在 helloword.thrift 里面定義函數(shù)及參數(shù)。
在服務(wù)端運行代碼?thrift?-r?--gen py?helloworld.thrift?
重新生成 gen-py 文件夾,將里面的代碼拷貝到客戶端的服務(wù)器。
轉(zhuǎn)載于:https://www.cnblogs.com/iplus/p/4489940.html
總結(jié)
以上是生活随笔為你收集整理的CentOS thrift python demo的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IT行业 侃侃富士康
- 下一篇: POJ 3267 The Cow Lex