最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python thrift搭建服務(wù)端和客戶端測試程序

 更新時(shí)間:2018年01月17日 13:42:02   作者:magedu-Derek  
這篇文章主要為大家詳細(xì)介紹了python thrift搭建服務(wù)端和客戶端測試程序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文生動(dòng)簡潔介紹了如何通過python搭建一個(gè)服務(wù)端和客戶端的簡單測試程序。

一、簡介

thrift是一個(gè)軟件框架,用來進(jìn)行可擴(kuò)展且跨語言的服務(wù)的開發(fā)。它結(jié)合了功能強(qiáng)大的軟件堆棧和代碼生成引擎,以構(gòu)建在 C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, and OCaml 這些編程語言間無縫結(jié)合的、高效的服務(wù)。

二、安裝

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

三、準(zhǔn)備服務(wù)器端

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
#創(chuàng)建服務(wù)端
handler = HelloWorldHandler()
processor = HelloWorld.Processor(handler)
#監(jiān)聽端口
transport = TSocket.TServerSocket("localhost", 9090)
#選擇傳輸層
tfactory = TTransport.TBufferedTransportFactory()
#選擇傳輸協(xié)議
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
#創(chuàng)建服務(wù)端 
server = TServer.TSimpleServer(processor, transport, tfactory, pfactory) 
print "Starting thrift server in python..."
server.serve()
print "done!"

四、準(zhǔn)備客戶端

#!/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)
 #選擇傳輸層,這塊要和服務(wù)端的設(shè)置一致
 transport = TTransport.TBufferedTransport(transport)
 #選擇傳輸協(xié)議,這個(gè)也要和服務(wù)端保持一致,否則無法通信
 protocol = TBinaryProtocol.TBinaryProtocol(transport)
 #創(chuàng)建客戶端
 client = HelloWorld.Client(protocol)
 transport.open()

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

 print "client - say"
 msg = client.say("Hello!")
 print "server - " + msg
 #關(guān)閉傳輸
 transport.close()
#捕獲異常
except Thrift.TException, ex:
 print "%s" % (ex.message)

PS.這個(gè)就是thrift的服務(wù)端和客戶端的實(shí)現(xiàn)小案例。一般只有在多種語言聯(lián)合開發(fā)時(shí)才會(huì)用到,如果是一種語言的話,thrift就沒有用武之地了。在多語言開發(fā)時(shí),我們拿到其他語言的thrift文件,就可以直接使用我們的python作為客戶端去調(diào)用thrift中的函數(shù)就可以了,或者我們提供thrift服務(wù)端文件供別的語言調(diào)用,總起來說還是很方便的,希望上面的例子可以讓大家明白thrift的簡單應(yīng)用!

相關(guān)文章

最新評(píng)論

宜章县| 镇巴县| 崇阳县| 铁岭县| 通榆县| 东阳市| 博罗县| 灵石县| 屏东县| 子长县| 友谊县| 芷江| 团风县| 华亭县| 洛隆县| 青浦区| 福安市| 柘城县| 乐陵市| 凤阳县| 余江县| 阜阳市| 巴林左旗| 临江市| 黎城县| 平塘县| 神木县| 遂宁市| 吉水县| 尚志市| 新晃| 淳化县| 漠河县| 措勤县| 衡阳市| 慈溪市| 阆中市| 石林| 香港| 遵义市| 荣昌县|