thrift安装及python和c++版本调试
一、安装过程
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文件,产生gen-py目录
]# thrift --gen python RecSys.thrift

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,c++版本
1.使用thrift生成c++文件,产生gen-cpp目录
//在schema目录下执行
]# thrift --gen cpp RecSys.thrift
2.进入gen-cpp目录,编译生成server的bin文件
]# g++ -g -Wall -I/usr/local/include/thrift RecSys_constants.cpp RecSys.cpp RecSys_server.skeleton.cpp RecSys_types.cpp -lthrift -o server
3.修改server代码(RecSys_server.skeleton.cpp)
23 void rec_data(std::string& _return, const std::string& data) {
24 // Your implementation goes here
25 std::cout << "Recevie data: " << data << std::endl;
26 _return = "I'm OK !!!";
27 }
4.执行server代码
]# ./server
//如果执行报错: libthrift-0.9.3.so: cannot open shared object file: No such file or directory
解决方法:
]# vim /etc/ld.so.conf
在末尾添加下面一行
/usr/local/lib/
然后再执行:
]# ldconfig
5.执行python的client端代码
//python的client端调用c++的server端
]# python client.py
6.可以写一个Makefile文件来编译生成server的bin文件
在gen-cpp目录下创建一个Makefile文件
GXX = g++
FLAGS = -g -Wall
INCLUDES = -I/usr/local/include/thrift
LIBS = -L/usr/local/lib/*.so -lthrift
SERVER_OBJECTS = RecSys_constants.cpp RecSys.cpp RecSys_server.skeleton.cpp RecSys_types.cpp
CLIENT_OBJECTS = RecSys.cpp client.cpp
server:
$(GXX) $(INCLUDES) $(SERVER_OBJECTS) $(LIBS) -o server
client:
$(GXX) $(INCLUDES) $(CLIENT_OBJECTS) $(LIBS) -o client
.PHONY: clean
clean:
rm -rf server
//备注:.PHONY: clean 是为了防止当前目录下有clean同名文件,导致clean删除命令无法执行
7.编写c++的client端代码
#include "RecSys.h"
#include <iostream>
#include <string>
#include <transport/TSocket.h>
#include <transport/TBufferTransports.h>
#include <protocol/TBinaryProtocol.h>
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace std;
int main(int argc, char **argv) {
boost::shared_ptr<TSocket> socket(new TSocket("localhost", 9090));
boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
transport->open();
RecSysClient client(protocol);
string send_data = "can you help me ???";
string recevie_data;
client.rec_data(recevie_data, send_data);
cout << "Send data:" << send_data << endl;
cout << "Receive data:" << recevie_data << endl;
transport->close();
}
thrift安装及python和c++版本调试的更多相关文章
- 【python】如何查看已经安装的python软件包和版本
pip 是一个安装和管理 Python 包的工具 , 是 easy_install 的一个替换品. pip freeze可以查看已经安装的python软件包和版本 pip list 也可以
- Pycharm选择pyenv安装的Python版本
在macOS上使用pyenv实现Python多版本共存后,pyenv安装的Python版本存在于macOS下的 ~/.pyenv/versions/下. 在Pycharm时,选择此目录下对应的版本即可 ...
- windows安装多个python及pip版本
windows安装多个python及pip版本 1.下载所需要的python2和python3安装包 2.一路next 3.设置环境变量 4.修改python安装目录下的可执行程序名称 5.在cmd中 ...
- 安装的 Python 版本太多互相干扰?pyenv 建议了解一下。
写在之前 我们都知道现在的 Python 有 Python2 和 Python3,但是由于各种乱七八糟的原因导致这俩哥们要长期共存,荣辱与共,尴尬的是这哥俩的差异还比较大,在很多时候我们可能要同时用到 ...
- python的多版本安装以及常见错误(长期更新)
(此文长期更新)Python安装常见错误汇总 注:本教程以python3.6为基准 既然是总结安装过程中遇到的错误,就顺便记录一下我的安装过程好了. 先来列举一下安装python3.6过程中可能需要的 ...
- 查看python和NumPy版本和安装路径
记录查看Python和NumPy版本以及路径的几条命令 # 查看Python版本及路径 python -V python -c "import sys;print(sys.executabl ...
- Python的安装和配置(windowns 双版本)
1.去官网上下载python,注意版本. 官网地址:https://www.python.org/downloads/windows 2.下载安装版或者zip包都可以.安装就按向导一步一步完成即可.z ...
- windows 如何将安装Anaconda之前已经安装的python版本(中已安装的库)移动到 Anaconda中
题目]如何将安装Anaconda之前已经安装的python版本(中已安装的库)移动到 Anaconda中 一.概述 之前安装tensorflow的安装了anaconda并用它进行安装,anaconda ...
- thrift安装及常见问题
一.安装thrift (macOS / Linux) 1. 下载thrift0.10.0源码 https://github.com/apache/thrift/releases/tag/0.10.0 ...
随机推荐
- node的第一步,hello,以及小技巧和CPU使用情况。到底能用几个核心?
安装了啥的就不说了,百度一下有很多. Windows环境.Linux不会,所有就不说了. 1. hello Word node的hello Word很简单,就一行. console.log(&quo ...
- 史上最全的Spring-Boot-Starter开发手册
1.前面的话 我们都知道可以使用 SpringBoot 快速的开发基于 Spring 框架的项目.由于围绕 SpringBoot 存在很多开箱即用的 Starter 依赖,使得我们在开发业务代码时能够 ...
- vue页面固定锁死
- linux系统设置cpu孤立
介绍 针对cpu密集型的任务,消耗cpu较高,最好设置cpu亲和度,以提高任务执行效率,避免cpu进行上下文切换,浪费不必要的性能. 特定任务(进程/线程)需要独占一个cpu核心并且不想让其他任务(进 ...
- 【zabbix教程系列】四、用户自定义监控
本篇介绍运用zabbix进行自定义监控,以系统用户登录数量为例. 一.zabbix自定义语法 UserParameter=<key>,<shell command> 二.age ...
- windows环境下mysql密码重置
1.打开cmd窗口,输入命令[mysqld --skip-grant-tables]回车. 2.再打开一个cmd窗口,输入命令[mysql]回车. 3.输入命令[use mysql; ] 连接权限数据 ...
- Windows Server 2012 蓝屏 Wpprecorder.sys 故障
坑爹的园区昨天停电了,导致运行中的服务器中断,来电之后,其中有一台Windows 系统的服务无法运行了,接了个显示器,发现无法进入系统了,挂掉了,这下可完蛋了,虽然做了Radio 磁盘阵列,数据不会丢 ...
- P4137 Rmq Problem / mex (莫队)
题目 P4137 Rmq Problem / mex 解析 莫队算法维护mex, 往里添加数的时候,若添加的数等于\(mex\),\(mex\)就不能等于这个值了,就从这个数开始枚举找\(mex\): ...
- IntelliJ IDEA 创建 Maven简单项目
创建简单Maven项目 使用IDEA提供的Maven工具,根据artifact创建简单Maven项目.根据下图操作,创建Maven项目. 使用IDEA提供的Maven工具创建的Maven简单项目目录结 ...
- python并发编程之IO阻塞基础知识点
IO模型 解决IO问题的方式方法 问题是:IO操作阻塞程序执行 解决的也仅仅是网络IO操作 一般数据传输经历的两个阶段,如图: IO阻塞模型分类: 阻塞IO 非阻塞IO 多路复用IO 异步IO(爬 ...