python thrift搭建服务端和客户端测试程序

yipeiwu_com5年前Python基础

本文生动简洁介绍了如何通过python搭建一个服务端和客户端的简单测试程序。

一、简介

thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发。它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, and OCaml 这些编程语言间无缝结合的、高效的服务。

二、安装

1.下载地址

 http://www.apache.org/dyn/closer.cgi?path=/thrift/0.9.2/thrift-0.9.2.tar.gz

2.安装

 [root@localhost ~]# yum -y groupinstall "Development Tools"
[root@localhost ~]# yum -y install libevent-devel zlib-devel openssl-devel autoconf automake
[root@localhost ~]# wget http://ftp.gnu.org/gnu/bison/bison-2.5.1.tar.gz 
[root@localhost ~]# tar xf bison-2.5.1.tar.gz
[root@localhost ~]# cd bison-2.5.1
[root@localhost ~]# ./configure --prefix=/usr
[root@localhost ~]# make
[root@localhost ~]# make install
[root@localhost ~]# tar xf thrift-0.9.2.tar.gz 
[root@localhost ~]# cd thrift-0.9.2
[root@localhost thrift-0.9.2]# ./configure -with-lua=no

3.安装python插件

pip install thrift

三、准备服务器端

1.编辑接口文件helloworld.thrift:

#!/usr/bin/env python 
import socket
import sys
sys.path.append('./gen-py') 
from helloworld import HelloWorld 
from helloworld.ttypes import *
 from thrift.transport import TSocket
 from thrift.transport import TTransport
 from thrift.protocol import TBinaryProtocol
 from thrift.server import TServer
 class HelloWorldHandler: 
   def ping(self):  
     return "pong"  
   def say(self, msg):
    ret = "Received: " + msg  
   print ret  
   return ret
#创建服务端
handler = HelloWorldHandler()
processor = HelloWorld.Processor(handler)
#监听端口
transport = TSocket.TServerSocket("localhost", 9090)
#选择传输层
tfactory = TTransport.TBufferedTransportFactory()
#选择传输协议
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
#创建服务端 
server = TServer.TSimpleServer(processor, transport, tfactory, pfactory) 
print "Starting thrift server in python..."
server.serve()
print "done!"

四、准备客户端

#!/usr/bin/env python

import sys
sys.path.append('./gen-py')

from helloworld import HelloWorld #引入客户端类

from thrift import Thrift 
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol

try:
 #建立socket
 transport = TSocket.TSocket('localhost', 9090)
 #选择传输层,这块要和服务端的设置一致
 transport = TTransport.TBufferedTransport(transport)
 #选择传输协议,这个也要和服务端保持一致,否则无法通信
 protocol = TBinaryProtocol.TBinaryProtocol(transport)
 #创建客户端
 client = HelloWorld.Client(protocol)
 transport.open()

 print "client - ping"
 print "server - " + client.ping()

 print "client - say"
 msg = client.say("Hello!")
 print "server - " + msg
 #关闭传输
 transport.close()
#捕获异常
except Thrift.TException, ex:
 print "%s" % (ex.message)

PS.这个就是thrift的服务端和客户端的实现小案例。一般只有在多种语言联合开发时才会用到,如果是一种语言的话,thrift就没有用武之地了。在多语言开发时,我们拿到其他语言的thrift文件,就可以直接使用我们的python作为客户端去调用thrift中的函数就可以了,或者我们提供thrift服务端文件供别的语言调用,总起来说还是很方便的,希望上面的例子可以让大家明白thrift的简单应用!

相关文章

Python API 自动化实战详解(纯代码)

Python API 自动化实战详解(纯代码)

主要讲如何在公司利用Python 搞API自动化。 1.分层设计思路 dataPool :数据池层,里面有我们需要的各种数据,包括一些公共数据等 config :基础配置 tools :...

python执行get提交的方法

本文实例讲述了python执行get提交的方法。分享给大家供大家参考。具体如下: import sys, urllib2, urllib def addGETdata(url, da...

Python translator使用实例

1.string.maketrans设置字符串转换规则表(translation table) allchars = string.maketrans('...

Python编程中的文件读写及相关的文件对象方法讲解

python文件读写 python 进行文件读写的内建函数是open或file file_hander(文件句柄或者叫做对象)= open(filename,mode) mode: 模式...

Appium+python自动化怎么查看程序所占端口号和IP

Appium+python自动化怎么查看程序所占端口号和IP

简介 这篇博文和分类看似没有多大关系,但是也是从上一篇衍生出来的产物,因为涉及到 FQ工具 Lantern ,就算是给关注和支持的小伙伴们拓展一下眼界和知识面。而且好多人都阅读了上一篇...