python使用thrift教程的方法示例

yipeiwu_com7年前Python基础

一、前言:  

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不匹配

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python中super关键字用法实例分析

本文实例讲述了Python中super关键字用法。分享给大家供大家参考。具体分析如下: 在Python类的方法(method)中,要调用父类的某个方法,在Python 2.2以前,通常的...

Python Paramiko模块的安装与使用详解

一、前言 常见的解决方法都会需要对远程服务器必要的配置,如果远程服务器只有一两台还好说,如果有N台,还需要逐台进行配置,或者需要使用代码进行以上操作时,上面的办法就不太方便了。而使用pa...

python实现学生信息管理系统

继上篇博客Python实现简易通讯录后,我就想写一个复杂点的学生信息管理系统,这次实现的功能有 1.学生信息的录入管理;   2.学生选课操作;   3.学生选课情况查询; 这次仍然...

解决Python运行文件出现out of memory框的问题

解决Python运行文件出现out of memory框的问题

爬虫过程中,发现pycharm变得非常卡,然后出现了这个框: 原本想的是4G内存不够,带不动程序,要加内存条。然后发现图中三个对话框的数字都可以改动,感叹号右边也说please inc...

理解Python中函数的参数

 定义函数的时候,我们把参数的名字和位置确定下来,函数的接口定义就完成了。对于函数的调用者来说,只需要知道如何传递正确的参数,以及函数将返回什么样的值就够了,函数内部的复杂逻辑...