网站首页 > 技术文章 正文
thrift-0.12.0 python3.4.3
Thrift 简介:
Thrift 是一款高性能、开源的 RPC 框架,产自 Facebook 后贡献给了 Apache,Thrift 囊括了整个 RPC 的上下游体系,自带序列化编译工具,因为 Thrift 采用的是二进制序列化,并且与 gRPC 一样使用的都是长连接建立 client 与 server 之间的通讯,相比于比传统的使用XML,JSON,SOAP等短连接的解决方案性能要快得多。
本篇只介绍 Python 关于 Thrift 的基础使用。
安装
- 安装 Thrift 的 python 库有两种方案:
通过 pip 命令安装,缺点必须要有外网或者内网的 pip 源:$ pip install thrift
- 通过源码安装:从 github 上下载 thrift 0.10.0 的源码 ,解压后进入 thrift-0.10.0/lib/py 目录执行:$ python setup.py install
- 安装 Thrift 的 IDL 编译工具windows 平台下安装:直接下载:thrift complier 下载地址,下载完成后改名为:thrift.exe 并将其放入到系统环境变量下即可使用
- Linux 平台下安装:
从 github 上下载 thrift 0.10.0 的源码,解压后进入:thrift-0.10.0/compiler/cpp 目录执行如下命令完成编译后,将其放入到系统环境变量下即可使用:
$ mkdir cmake-build
$ cd cmake-build
$ cmake ..
$ make
- 验证是否安装成功:
$ thrift -version,如果打印出来:Thrift version 0.10.0 表明 complier 安装成功
实践:
下面我们使用 Thrift 定义一个接口,该接口实现对传入的数据进行大写的格式化处理。
- 创建 python 项目 thrift_demo 工程:
Paste_Image.png
client目录下的 client.py 实现了客户端用于发送数据并打印接收到 server 端处理后的数据
- server 目录下的 server.py 实现了服务端用于接收客户端发送的数据,并对数据进行大写处理后返回给客户端
- thrift_file 用于存放 thrift 的 IDL 文件: *.thrift
- 定义 Thrift RPC 接口
example.thrift:
namespace py example
struct Data {
1: string text
}
service format_data {
Data do_format(1:Data data),
}
- 编译 thrift 文件
进入 thrift_file 目录执行:$ thrift -out .. --gen py example.thrift,就会在 thrift_file 的同级目录下生成 python 的包:example
Paste_Image.png
- 实现 server 端:
server.py:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'xieyanke'
from example import format_data
from example import ttypes
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
__HOST = 'localhost'
__PORT = 8080
class FormatDataHandler(object):
def do_format(self, data):
return ttypes.Data(data.text.upper())
if __name__ == '__main__':
handler = FormatDataHandler()
processor = format_data.Processor(handler)
transport = TSocket.TServerSocket(__HOST, __PORT)
tfactory = TTransport.TBufferedTransportFactory()
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
rpcServer = TServer.TSimpleServer(processor,transport, tfactory, pfactory)
print('Starting the rpc server at', __HOST,':', __PORT)
rpcServer.serve()
- 实现 client 端:
client.py:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from example.format_data import Client
from example.format_data import Data
__HOST = 'localhost'
__PORT = 8080
tsocket = TSocket.TSocket(__HOST, __PORT)
transport = TTransport.TBufferedTransport(tsocket)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = Client(protocol)
data = Data('hello,world!')
transport.open()
print(client.do_format(data).text)
- 执行验证结果:
先启动 server,之后再执行 client
- client 侧控制台如果打印的结果为: HELLO,WORLD! ,证明 Thrift 的 RPC 接口定义成功
注意
import sys
sys.path.append(r"X:\xx\thrift_demo") #thrift_demo 目录的绝对路径
猜你喜欢
- 2025-04-09 ScalersTalk成长会Python小组第17周学习笔记
- 2025-04-09 78行Python代码帮你复现微信撤回消息!
- 2025-04-09 利用gRPC构建Python微服务(二)-gRPC基础
- 2025-04-09 Python 网络编程的基础复习:理解Socket的作用
- 2025-04-09 python之list(set())函数(python的list函数)
- 2025-04-09 Python Queue 进阶用法(python queue库)
- 2025-04-09 python 模块 multiprocessing(python multiprocessor)
- 2025-04-09 用Python编写FPGA以太网MAC(附源码下载方式)
- 2025-04-09 当前从 Python 调用 C/C++ 代码的有多少种方法、最佳方案是什么?
- 2025-04-09 Scapy:用Python编写自己的网络抓包工具
- 263℃Python短文,Python中的嵌套条件语句(六)
- 263℃python笔记:for循环嵌套。end=""的作用,图形打印
- 261℃PythonNet:实现Python与.Net代码相互调用!
- 256℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 256℃Python实现字符串小写转大写并写入文件
- 116℃原来2025是完美的平方年,一起探索六种平方的算吧
- 96℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 89℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)