python thrift搭建服務(wù)端和客戶端測試程序
本文生動(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í)才會用到,如果是一種語言的話,thrift就沒有用武之地了。在多語言開發(fā)時(shí),我們拿到其他語言的thrift文件,就可以直接使用我們的python作為客戶端去調(diào)用thrift中的函數(shù)就可以了,或者我們提供thrift服務(wù)端文件供別的語言調(diào)用,總起來說還是很方便的,希望上面的例子可以讓大家明白thrift的簡單應(yīng)用!
- python thrift 實(shí)現(xiàn) 單端口多服務(wù)的過程
- python3.7通過thrift操作hbase的示例代碼
- python使用thrift教程的方法示例
- python利用thrift服務(wù)讀取hbase數(shù)據(jù)的方法
- python 如何用urllib與服務(wù)端交互(發(fā)送和接收數(shù)據(jù))
- Python連接Java Socket服務(wù)端的實(shí)現(xiàn)方法
- python 實(shí)現(xiàn)客戶端與服務(wù)端的通信
- python網(wǎng)絡(luò)編程socket實(shí)現(xiàn)服務(wù)端、客戶端操作詳解
- Python Websocket服務(wù)端通信的使用示例
- python實(shí)現(xiàn)Thrift服務(wù)端的方法
相關(guān)文章
Python range與enumerate函數(shù)區(qū)別解析
這篇文章主要介紹了Python range與enumerate函數(shù)區(qū)別解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02基于Python實(shí)現(xiàn)傻瓜式GIF制作工具
有沒有什么內(nèi)容形式,比小視頻更小,比普通圖片更豐富?有,GIF動(dòng)態(tài)圖就是其中一種形式。本文將為大家介紹如何通過Python實(shí)現(xiàn)一個(gè)傻瓜式的gif生成工具,感興趣的可以了解一下2021-12-12Python如何實(shí)現(xiàn)自動(dòng)發(fā)送郵件
對于一些每天需要發(fā)的報(bào)表或者是需要一次發(fā)送多份的報(bào)表,我們可以考慮借助Python來自動(dòng)發(fā)送郵件。本文主要介紹了如何利用Python實(shí)現(xiàn)自動(dòng)發(fā)送郵件,感興趣的小伙伴可以了解一下2021-11-11scrapy結(jié)合selenium解析動(dòng)態(tài)頁面的實(shí)現(xiàn)
這篇文章主要介紹了scrapy結(jié)合selenium解析動(dòng)態(tài)頁面的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09python中numpy數(shù)組與list相互轉(zhuǎn)換實(shí)例方法
在本篇文章里小編給大家整理的是一篇關(guān)于python中numpy數(shù)組與list相互轉(zhuǎn)換實(shí)例方法,對此有興趣的朋友們可以學(xué)習(xí)下。2021-01-01Python urls.py的三種配置寫法實(shí)例詳解
這篇文章主要介紹了Python urls.py的三種配置寫法實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04