您的位置:首页 > 脚本大全 > > 正文

pythonmath库引入方法(python使用thrift教程的方法示例)

更多 时间:2021-11-08 14:12:04 类别:脚本大全 浏览量:2780

pythonmath库引入方法

python使用thrift教程的方法示例

一、前言:  

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

  • ?
  • 1
  • 2
  • 3
  • 4
  • 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 代码

    pythonmath库引入方法(python使用thrift教程的方法示例)

    生成如下结构

    pythonmath库引入方法(python使用thrift教程的方法示例)

    2.然后将生成的 python 代码 和 文件,放到新建的 python 项目中。完成后先运行服务器代码。

    服务端代码 server.py:

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 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

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 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
    您可能感兴趣