python thrift 示例<转>
来源:互联网 发布:个人云计算 编辑:程序博客网 时间:2024/05/16 02:51
转自: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
0 0
- python thrift 示例<转>
- python thrift 示例
- Python Thrift示例
- Thrift in python<转>
- thrift for python部署<转>
- python是通过thrift来操作hbase的示例程序
- thrift python
- Python thrift
- thrift使用示例二
- Thrift示例分析
- thrift远程调用示例
- thrift 安装及示例
- thrift使用入门示例
- Thrift开发示例
- thrift远程调用示例
- Thrift示例程序
- Thrift第一个示例
- Thrift使用示例
- 走在机房重构的路上之dt表头
- 谷歌笔试题(最长公共子串)
- uva 301
- Cocos2d-x Sprite 执行 CCFade~ 这类透明度变化动作的一些有关问题
- jslinux 对应到 linux kernel 3.8.5 上
- python thrift 示例<转>
- 关于/etc/hosts文件
- poj 3208 Apocalypse Someday (数位dp)
- Java异常:选择Checked Exception还是Unchecked Exception?
- Qt QDialogButtonBox 英文翻译问题
- HDU3240-Counting Binary Trees(Catalan数+求逆元(非互质))
- windows cmd命令下的最基本的压缩解压
- Android应用请求获取Root权限
- Java 覆盖私有方法