python thrift 服务端与客户端使用

简介:

一、简介

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

二、安装

    1.下载地址    

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

    2.安装

1
2
3
4
5
6
7
8
9
10
11
[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插件

1
pip  install  thrift

三、准备服务器端

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

1
2
3
4
service HelloWorld {
     string  ping (),
     string say(1:string msg)
}

    2.编辑 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
#!/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!"

四、准备客户端

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
#!/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:亲测通过,吐槽一下,这东西以前都没有听说过,就要拿来开发,还要只是用客户端



本文转自wangfeng7399 51CTO博客,原文链接:http://blog.51cto.com/wangfeng7399/1696108,如需转载请自行联系原作者


相关文章
|
4月前
|
Python
Socket学习笔记(二):python通过socket实现客户端到服务器端的图片传输
使用Python的socket库实现客户端到服务器端的图片传输,包括客户端和服务器端的代码实现,以及传输结果的展示。
215 3
Socket学习笔记(二):python通过socket实现客户端到服务器端的图片传输
|
8月前
|
XML 物联网 API
服务端和客户端 RESTful 接口上传 Excel 的 Python 代码
本文作者木头左是物联网工程师,分享如何使用 Python 和 Flask-RESTful 构建一个简单的 RESTful API,实现文件上传功能,特别支持Excel文件。通过安装Flask和Flask-RESTful库,创建Flask应用,实现文件上传接口,并将其添加到API。该方法具有简单易用、灵活、可扩展及社区支持等优点。
服务端和客户端 RESTful 接口上传 Excel 的 Python 代码
|
4月前
|
JSON 数据格式 Python
Socket学习笔记(一):python通过socket实现客户端到服务器端的文件传输
本文介绍了如何使用Python的socket模块实现客户端到服务器端的文件传输,包括客户端发送文件信息和内容,服务器端接收并保存文件的完整过程。
243 1
Socket学习笔记(一):python通过socket实现客户端到服务器端的文件传输
|
5月前
|
关系型数据库 MySQL Python
mysql之python客户端封装类
mysql之python客户端封装类
|
6月前
|
网络协议 安全 Unix
6! 用Python脚本演示TCP 服务器与客户端通信过程!
6! 用Python脚本演示TCP 服务器与客户端通信过程!
108 1
|
6月前
|
传感器 数据采集 算法
python实现ModBusRTU客户端方式
python实现基于串口通信的ModBusRTU客户端是一件简单的事情,只要通过pymodbus模块就可以实现。
|
6月前
|
开发者 Python
深入解析Python `httpx`源码,探索现代HTTP客户端的秘密!
深入解析Python `httpx`源码,探索现代HTTP客户端的秘密!
131 1
|
7月前
|
传感器 数据采集 算法
python实现ModBusRTU客户端方式
python实现基于串口通信的ModBusRTU客户端是一件简单的事情,只要通过pymodbus模块就可以实现。
124 22
|
8月前
|
JSON 数据格式 Python
Python 的 requests 库是一个强大的 HTTP 客户端库,用于发送各种类型的 HTTP 请求
【6月更文挑战第15天】Python的requests库简化了HTTP请求。安装后,使用`requests.get()`发送GET请求,检查`status_code`为200表示成功。类似地,`requests.post()`用于POST请求,需提供JSON数据和`Content-Type`头。
71 6
|
8月前
|
JSON 数据格式 Python
python3 服务端使用CGI脚本处理POST的Json数据
python3 服务端使用CGI脚本处理POST的Json数据
92 6