





























下载源码包
wget http://apache.fayea.com/thrift/0.9.3/thrift-0.9.3.tar.gz
安装thrift [进入thrift目录]
yum install openssl-devel.x86_64 [可选择是否安装]yum install boost-devel.x86_64 [必须安装]./configure --with-cpp --with-boost --with-python --without-csharp --with-java --without-erlang --without-perl --with-php --without-php_extension --without-ruby --without-haskell --without-goyum install boost-devel-staticmake,make isntall安装thrift对python的支持模块
pip install thrift==0.9.3
service RecSys {
string rec_data(1:string data)
}
thrift --gen py RecSys.thrift,此时在当前目录中会产生gen-py的文件夹,其中有当前的模块名字import sys
sys.path.append('../schema/gen-py/')
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from RecSys import RecSys
from thrift.server import TServer
from RecSys.ttypes import *
class RecSysHandler(RecSys.Iface):
def rec_data(self, a):
print "Receive: %s" %(a)
return "I`m OK !!!"
if __name__ == "__main__":
# 实例化handler
handler = RecSysHandler()
# 设置processor
processor = RecSys.Processor(handler)
# 设置端口
transport = TSocket.TServerSocket('localhost', port=9900)
# 设置传输层
tfactory = TTransport.TBufferedTransportFactory()
# 设置传输协议
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
handler = RecSysHandler()
server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
print 'Starting the server...'
server.serve()
print 'done'
#coding=utf-8
import sys
sys.path.append('../schema/gen-py/')
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from RecSys import RecSys
if __name__ == "__main__":
# 设置端口
transport = TSocket.TSocket('localhost', port=9900)
# 设置传输层
transport = TTransport.TBufferedTransport(transport)
# 设置传输协议
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = RecSys.Client(protocol)
transport.open()
rs = client.rec_data("are you ok ??")
print "receive return data: ", rs
transport.close()
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。