zoukankan      html  css  js  c++  java
  • python thrift 示例

    转自: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 python
    
    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 TServer
     
    import socket
    
    class 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 python
    
    import sys
    sys.path.append('./gen-py')
    
    from helloworld import HelloWorld
    from helloworld.ttypes import *
    from helloworld.constants import *
    
    from thrift import Thrift
    from thrift.transport import TSocket
    from thrift.transport import TTransport
    from thrift.protocol import TBinaryProtocol
    
    try:
      # 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
  • 相关阅读:
    绝对定位和相对定位的内幕
    水平居中和垂直居中
    玩转html5<canvas>画图
    基本排序算法
    很好用的canvas
    IE浏览器存在的setAttribute bug
    js 高程 函数节流 throttle() 分析与优化
    js apply()、call() 使用参考
    js 高程 22.1.4 函数绑定 bind() 封装分析
    事件处理程序中 this 的指向
  • 原文地址:https://www.cnblogs.com/lovemdx/p/2782180.html
Copyright © 2011-2022 走看看