thrift的使用--python
转载blog:Http://www.cnblogs.com/pinking/p/7726478.html
在这里要补充一点的就是在
在这里python要安装thrift包时候,可以直接在安装好的thrift好的模块中sudo Python setup.py install安装就可以,
java、c++的我暂时没走通,周末来摸索
以下原文,作者在windows上面,我在linux上面:
1、下载thrift,下载地址:http://arcHive.apache.org/dist/thrift/0.9.3/
2、在编写python的thrift代码时,需要先安装thrift module,下载路径:https://pypi.python.org/pypi/thrift/0.9.1
3、安装thrift:
tar -zvxf thrift-0.9.3.tar.gz 直接下一步。。。
:在命令行中输入:
y]$ thrift --version
Thrift version 0.9.3
即可以看到安装后的版本信息。
4、thrift 采用IDL(Interface Definition Language)来定义通用的服务接口,并通过生成不同的语言代理实现来达到跨语言、平台的功能。在thrift的IDL中可以定义以下一些类型:基本数据类型,结构体,容器,异常、服务。
thrift脚本helloworld.thrift:
const string HELLO_YK = "yk"
service HelloWorld {
void ping(),
string sayHello(),
string sayMsg(1:string msg)
}
thrift脚本通过Thrift编辑器生成所要求的python开发语言代码。即:
thrift -r --gen py helloworld.thrift
5、Thrift是一个典型的CS结构,客户端和服务端可以使用不同的语言开发。本文以python为例:
PythonServer.py:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | 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_YK) print msg transport.close() except Thrift.TException, tx: print "%s" % (tx.message) |
运行结果:
server端:
python PythonServer.py
Starting python server...
ping()
client端:
python PythonClient.py
ping()
say hello from 10.101.173.116
say yk from 10.101.173.116
相关文章