wget http://apache.fayea.com/thrift/0.9.3/thrift-0.9.3.tar.gz
php
centos:yum install gcc gcc-c++
java
若是没有安装g++,没法编译python
tar -xvf thrift-0.9.3.tar.gz
c++
进入thrift-0.9.3目录apache
运行命令:yum install boost-devel.x86_64
centos
运行命令:yum install boost-devel-static
ruby
运行命令:./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-go
服务器
编译,命令:make
app
编译完成以后安装,命令:make install
socket
出现thrift
命令提示表示Thrift安装成功
service RecSys { string rec_data(1:string data) }
#! /usr/bin/env python # -*- coding: utf-8 -*- import sys sys.path.append('gen-py') from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol from thrift.server import TServer from RecSys import RecSys from RecSys.ttypes import * class RecSysHandler(RecSys.Iface): def rec_data(self, a): print "Receive: %s" %(a) return "ok" if __name__ == "__main__": # 实例化handler handler = RecSysHandler() # 设置processor processor = RecSys.Processor(handler) # 设置端口 transport = TSocket.TServerSocket('localhost', port=9900) # 设置传输层 tfactory = TTransport.TBufferedTransportFactory() # 设置传输协议 pfactory = TBinaryProtocol.TBinaryProtocolFactory() server = TServer.TThreadedServer(processor, transport, tfactory, pfactory) print 'Starting the server...' server.serve() print 'done'
#! /usr/bin/env python # -*- coding: utf-8 -*- import sys sys.path.append("gen-py") // 将第4步骤产生的目录加载进来 from thrift import Thrift from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol from RecSys import RecSys # from demo.ttypes import * try: # Make Socket # 创建socket, IP 和port要写对 transport = TSocket.TSocket('localhost', 9900) # Buffering is critical. Raw sockets are very slow # 选择传输层,这块要和服务器的设置同样 transport = TTransport.TBufferedTransport(transport) # Wrap in a protocol # 选择传输协议,这个也要和服务器保持一致,负责没法通讯 protocol = TBinaryProtocol.TBinaryProtocol(transport) client = RecSys.Client(protocol) # Connect! transport.open() # Call server services rst = client.rec_data("are you ok!") print rst # close transport transport.close() except Thrift.TException, ex: print "%s" % (ex.message)
thrift --gen py RecSys.thrift
运行完以后会在当前目录生成一个gen-py
的文件夹,里面有模块的名字之类的东西
运行命令:python server.py
,启动服务端
运行命令:python client.py
,启动客户端,并能收到服务端发出的信息