python thrift 樣本<轉>__python

來源:互聯網
上載者:User

轉自:http://tkang.blogspot.com/2010/07/thrift-server-client-in-python.html

在編寫python的thrift代碼時,需要先安裝thrift module

$ cd thrift-root/lib/py/$ sudo python setup.py install

下面是一個python的例子 helloworld.thrift

 

const string HELLO_IN_KOREAN = "an-nyoung-ha-se-yo"const string HELLO_IN_FRENCH = "bonjour!"const string HELLO_IN_JAPANESE = "konichiwa!"service HelloWorld { void ping(), string sayHello(), string sayMsg(1:string msg)}


生產代碼

$ thrift -r --gen py helloworld.thrift

編寫伺服器PythonServer.py

#!/usr/bin/env pythonimport syssys.path.append('./gen-py') from helloworld import HelloWorldfrom helloworld.ttypes import *from thrift.transport import TSocketfrom thrift.transport import TTransportfrom thrift.protocol import TBinaryProtocolfrom thrift.server import TServer import socketclass HelloWorldHandler:  def __init__(self):    self.log = {}  def ping(self):    print "ping()"  def sayHello(self):    print "sayHello()"    return "say hello from " + socket.gethostbyname(socket.gethostname())  def sayMsg(self, msg):    print "sayMsg(" + msg + ")"    return "say " + msg + " from " + socket.gethostbyname(socket.gethostname())handler = HelloWorldHandler()processor = HelloWorld.Processor(handler)transport = TSocket.TServerSocket('127.0.0.1',30303)tfactory = TTransport.TBufferedTransportFactory()pfactory = TBinaryProtocol.TBinaryProtocolFactory()server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)print "Starting python server..."server.serve()print "done!"

編寫用戶端PythonClient.py

#!/usr/bin/env pythonimport syssys.path.append('./gen-py')from helloworld import HelloWorldfrom helloworld.ttypes import *from helloworld.constants import *from thrift import Thriftfrom thrift.transport import TSocketfrom thrift.transport import TTransportfrom thrift.protocol import TBinaryProtocoltry:  # Make socket  transport = TSocket.TSocket('127.0.0.1', 30303)  # Buffering is critical. Raw sockets are very slow  transport = TTransport.TBufferedTransport(transport)  # Wrap in a protocol  protocol = TBinaryProtocol.TBinaryProtocol(transport)  # Create a client to use the protocol encoder  client = HelloWorld.Client(protocol)  # Connect!  transport.open()  client.ping()  print "ping()"  msg = client.sayHello()  print msg  msg = client.sayMsg(HELLO_IN_KOREAN)  print msg  transport.close()except Thrift.TException, tx:  print "%s" % (tx.message)

運行程式

$ python PythonServer.py$ python PythonClient.py
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.