码迷,mamicode.com
首页 > 编程语言 > 详细

thrift安装及python和c++版本调试

时间:2018-06-28 01:00:19      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:thrift   build   buffer   lex   sage   代码   tar.gz   lan   sel   

一、安装过程

1.安装依赖库

]# yum install boost-devel-static libboost-dev libboost-test-dev libboost-program-options-dev libevent-dev automake libtool flex bison pkg-config g++ libssl-dev ant

2.安装thrift

先下载thrift-0.9.3.tar.gz,解压后进入thrift-0.9.3目录

//需要支持的语言用--with, 不需要支持的语言用--without, 像ruby等语言最好去掉,否则可能会有一些兼容问题
]# ./configure --with-cpp --with-boost --with-python --without-csharp --with-java --without-erlang --without-perl --without-php --without-php_extension --without-ruby --without-haskell  --without-go
]# make
]# make install
//成功会显示 BUILD SUCCESSFUL,通过thrift命令查看是否安装成功
]# thrift 
//安装Thrift的时候遇到,如下错误
#./configure --prefix=/usr/local/thrift
trhift configure: error: "Error: libcrypto required."
//解决办法:
//安装 openssl openssl-devel (centOS)
#yum -y install openssl openssl-devel
# ./configure --prefix=/usr/local/thrift

二、调通单机版thrift,python版本

1.安装依赖库

]# pip install thrift==0.9.3

2.编写schema文件

//创建schema目录,创建一个schema文件RecSys.thrift
[root@localhost schema]# cat RecSys.thrift 
service RecSys{
    string rec_data(1:string data)
}

3.使用thrift生成python文件

]# thrift --gen python RecSys.thrift
//产生gen-py目录

技术分享图片

4.开发python代码

// client代码:
#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


try:
    # 设置端口
    transport = TSocket.TSocket(‘localhost‘, port=9090)

    # 设置传输层
    transport = TTransport.TBufferedTransport(transport)

    # 设置传输协议
    protocol = TBinaryProtocol.TBinaryProtocol(transport)

    client = RecSys.Client(protocol)

    transport.open()

    rst = client.rec_data("are you ok!!!")
    print "receive return data: ", rst

    transport.close()

except Thrift.TException, ex:
    print "%s" % (ex.message)
// server 代码
#coding=utf=8

import sys
sys.path.append(‘../schema/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 "I‘m OK !!!"


if __name__ == "__main__":

    # 实例化handler
    handler = RecSysHandler()

    # 设置processor
    processor = RecSys.Processor(handler)

    # 设置端口
    transport = TSocket.TServerSocket(‘localhost‘, port=9090)

    # 设置传输层
    tfactory = TTransport.TBufferedTransportFactory()

    # 设置传输协议
    pfactory = TBinaryProtocol.TBinaryProtocolFactory()

    server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)

    print ‘Starting the server...‘
    server.serve()
    print ‘done.‘

thrift安装及python和c++版本调试

标签:thrift   build   buffer   lex   sage   代码   tar.gz   lan   sel   

原文地址:https://www.cnblogs.com/CoolJayson/p/9236641.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!