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,如需转载请自行联系原作者


相关文章
|
7月前
|
UED Python
python开发桌面客户端
Python是一种高级编程语言,具有简洁易读的语法和强大的功能。在Python中开发桌面客户端可以使用各种GUI库,如Tkinter、PyQt、wxPython等。这些库提供了创建窗口、按钮、文本框等界面元素的功能,以及处理用户交互事件的能力。通过使用这些库,开发人员可以构建出功能强大且易于使用的桌面应用程序。
|
4月前
|
数据库 Python
Python-ElasticSearch客户端的封装(聚合查询、统计查询、全量数据)
Python-ElasticSearch客户端的封装(聚合查询、统计查询、全量数据)
52 0
|
5月前
|
物联网 Python
如何通过示例在Python中使用Paho MQTT客户端?
如何通过示例在Python中使用Paho MQTT客户端?
76 2
如何通过示例在Python中使用Paho MQTT客户端?
|
5月前
|
网络协议 Python
153 python网络编程 - TCP客户端
153 python网络编程 - TCP客户端
16 0
|
8月前
|
网络协议 网络架构 Python
【从零学习python 】76.服务器与客户端:网络通信的关键组成部分
【从零学习python 】76.服务器与客户端:网络通信的关键组成部分
56 0
|
8月前
|
Java Python
基于Flask创建Python服务端,并调用Java客户端
基于Flask创建Python服务端,并调用Java客户端
|
8月前
|
JavaScript 前端开发 Python
基于Flask创建Python服务端,并调用JavaScript客户端
基于Flask创建Python服务端,并调用JavaScript客户端
|
8月前
|
C# Python
基于Flask创建Python服务端,并调用Python客户端、C#客户端
基于Flask创建Python服务端,并调用Python客户端、C#客户端
100 0
|
9月前
|
安全 网络安全 数据安全/隐私保护
Python通过IMAP实现邮箱客户端
Python通过IMAP实现邮箱客户端
222 0
|
Java Python
Thrift的服务器和客户端Python案例
Thrift的服务器和客户端Python案例
174 0

热门文章

最新文章