python使用thrift教程的方法示例
作者:三只松鼠 时间:2022-03-14 04:29:02
一、前言:
Thrift 是一种接口描述语言和二进制通信协议。以前也没接触过,最近有个项目需要建立自动化测试,这个项目之间的微服务都是通过 Thrift 进行通信的,然后写自动化脚本之前研究了一下。
需要定义一个xxx.thrift的文件, 来生成各种语言的代码,生成之后我们的服务提供者和消费者,都需要把代码引入,服务端把代码实现,消费者直接使用API的存根,直接调用。
和 http 相比,同属于应用层,走 tcp 协议。Thrift 优势在于发送同样的数据,request包 和 response包 要比 http 小很多,在整体性能上要优于 http 。
二、使用方法
环境准备:
从官网上下载 windows 版的 thrift.exe:http://archive.apache.org/dist/thrift/0.9.3/(我这里用的是0.9.3版本)
python版本:Python 3.7.1
pip3 install thrift
1.首先使用 thrift 之前需要定义一个 .thrift 格式的文件,比如 test.thrift
service Transmit {
string sayMsg(1:string msg);
string invoke(1:i32 cmd 2:string token 3:string data)
}
然后运行命令:thrift-0.9.3.exe -gen py test.thrift 生成 python 代码
生成如下结构
2.然后将生成的 python 代码 和 文件,放到新建的 python 项目中。完成后先运行服务器代码。
服务端代码 server.py:
import json
from test import Transmit
from test.ttypes import *
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
import socket
class TransmitHandler:
def __init__(self):
self.log = {}
def sayMsg(self, msg):
msg = json.loads(msg)
print("sayMsg(" + msg + ")")
return "say " + msg + " from " + socket.gethostbyname(socket.gethostname())
def invoke(self,cmd,token,data):
cmd = cmd
token =token
data = data
if cmd ==1:
return json.dumps({token:data})
else:
return 'cmd不匹配'
if __name__=="__main__":
handler = TransmitHandler()
processor = Transmit.Processor(handler)
transport = TSocket.TServerSocket('127.0.0.1', 8000)
tfactory = TTransport.TBufferedTransportFactory()
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
print("Starting python server...")
server.serve()
客户端代码 client.py
import sys
import jsonfrom test import Transmit
from test.ttypes import *
from test.constants import *
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
transport = TSocket.TSocket('127.0.0.1', 8000)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = Transmit.Client(protocol)
# Connect!
transport.open()
cmd = 2
token = '1111-2222-3333-4444'
data = json.dumps({"name":"zhoujielun"})
msg = client.invoke(cmd,token,data)
print(msg)
transport.close()
# 执行结果:cmd不匹配
来源:http://www.cnblogs.com/shenh/p/10529073.html
标签:python,thrift
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python运行脚本文件的三种方法实例
2022-07-08 11:10:21
![](https://img.aspxhome.com/file/2023/0/97300_0s.png)
游戏的用户体验营销小札
2009-08-30 15:13:00
ASP中页面限权访问的几种方法
2007-12-13 06:53:00
用Python Flask创建简洁高效的URL短链接服务
2022-10-12 16:21:49
Python使用plotly绘制数据图表的方法
2023-04-25 13:19:00
![](https://img.aspxhome.com/file/2023/1/82721_0s.png)
简单介绍各种浏览器中的本地存储方法
2012-04-26 16:37:34
django 邮件发送模块smtp使用详解
2021-09-24 23:04:15
Python 数据化运营之KMeans聚类分析总结
2023-11-13 02:27:26
![](https://img.aspxhome.com/file/2023/4/97404_0s.png)
实例详解Matlab 与 Python 的区别
2021-07-20 22:14:15
Python中使用item()方法遍历字典的例子
2023-01-28 13:38:57
![](https://img.aspxhome.com/file/2023/5/85325_0s.jpg)
python3处理含有中文的url方法
2021-04-10 02:42:45
![](https://img.aspxhome.com/file/2023/8/92318_0s.jpg)
windowns使用PySpark环境配置和基本操作
2021-04-12 06:43:06
![](https://img.aspxhome.com/file/2023/3/95123_0s.png)
python调用百度语音REST API
2022-09-16 18:19:07
剖析SQL Server 事务日志的收缩和截断
2009-01-15 13:04:00
PHP json格式和js json格式 js跨域调用实现代码
2023-11-22 05:20:18
javascript禁用Tab键脚本实例
2023-10-13 10:53:58
Python变量格式化输出实现原理解析
2023-02-12 23:13:51
Python批量解压&压缩文件夹的示例代码
2023-02-17 17:43:29
![](https://img.aspxhome.com/file/2023/8/93948_0s.png)
WEB2.0网页制作标准教程(5)head区的其他设置
2007-11-13 13:28:00
利用python的socket发送http(s)请求方法示例
2022-06-06 08:33:39
![](https://img.aspxhome.com/file/2023/1/68871_0s.png)