python實(shí)現(xiàn)Thrift服務(wù)端的方法
近期在項(xiàng)目中存在跨編程語(yǔ)言協(xié)作的需求,使用到了Thrift。本文將記錄用python實(shí)現(xiàn)Thrift服務(wù)端的方法。
環(huán)境準(zhǔn)備
- 根據(jù)自身實(shí)際情況下載對(duì)應(yīng)的Thrift編譯器,比如我在Windows系統(tǒng)上使用的是thrift-0.9.3.exe 。下載地址:http://archive.apache.org/dist/thrift/
- python安裝thrift庫(kù):pip install thrift
編寫(xiě).thrift文件
.thrift文件定義了Thrift服務(wù)端和Thrift客戶端的通信接口,在該文件中定義的接口需由服務(wù)端實(shí)現(xiàn),并可被客戶端調(diào)用。Thrift編譯器會(huì)調(diào)用.thrift文件生成不同語(yǔ)言的thrift代碼,用于之后實(shí)現(xiàn)thrift服務(wù)端或thrift客戶端。
.thrift文件的編寫(xiě)規(guī)則可參考Thrift白皮書(shū)。下面將以demo.thrift文件舉例
service DemoService{ string ping(1:string param) map<i32,string> get_int_string_mapping_result(1:i32 key, 2:string value) bool get_bool_result() }
生成python對(duì)應(yīng)的thrift代碼
使用以下命令可以生成不同語(yǔ)言的thrift代碼:
thrift --gen <language> <Thrift filename>
通過(guò)thrift-0.9.3.exe --gen py demo.thrift 命令生成python版本的thrift文件,文件夾為gen-py,如下所示:
編寫(xiě)服務(wù)端
編寫(xiě)服務(wù)端server.py,用于實(shí)現(xiàn)在demo.thrift文件中定義的接口功能。
from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol from thrift.server import TServer import sys sys.path.append("./gen-py/") from demo import DemoService import random class DemoServer: def __init__(self): self.log = {} def ping(self, param): return "echo:" + param def get_int_string_mapping_result(self, key, value): return {key: value} def get_bool_result(self): return random.choice([True, False]) if __name__ == '__main__': # 創(chuàng)建處理器 handler = DemoServer() processor = DemoService.Processor(handler) # 監(jiān)聽(tīng)端口 transport = TSocket.TServerSocket(host="0.0.0.0", port=9999) # 選擇傳輸層 tfactory = TTransport.TBufferedTransportFactory() # 選擇傳輸協(xié)議 pfactory = TBinaryProtocol.TBinaryProtocolFactory() # 創(chuàng)建服務(wù)端 server = TServer.TThreadPoolServer(processor, transport, tfactory, pfactory) # 設(shè)置連接線程池?cái)?shù)量 server.setNumThreads(5) # 啟動(dòng)服務(wù) server.serve()
編寫(xiě)客戶端用于測(cè)試
編寫(xiě)客戶端client.py,用于測(cè)試服務(wù)端功能是否可用。
from thrift import Thrift from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol import sys sys.path.append("./gen-py/") from demo import DemoService if __name__ == '__main__': transport = TSocket.TSocket('127.0.0.1', 9999) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = DemoService.Client(protocol) # 連接服務(wù)端 transport.open() recv = client.ping("test") print(recv) recv = client.get_int_string_mapping_result(10, "MyThrift") print(recv) recv = client.get_bool_result() print(recv) # 斷連服務(wù)端 transport.close()
編寫(xiě)完成后,整個(gè)項(xiàng)目結(jié)構(gòu)如下圖所示:
測(cè)試服務(wù)端
運(yùn)行服務(wù)端server.py后,運(yùn)行客戶端client.py,打印的內(nèi)容如下:
echo:test {10: 'MyThrift'} True
此時(shí)客戶端能夠正常調(diào)用服務(wù)端所提供的接口。(PS:在調(diào)試過(guò)程中,也許需要修改gen-py文件夾中Thrift編譯器生成的python代碼)
以上就是python實(shí)現(xiàn)Thrift服務(wù)端的方法的詳細(xì)內(nèi)容,更多關(guān)于python實(shí)現(xiàn)Thrift服務(wù)端的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- python thrift搭建服務(wù)端和客戶端測(cè)試程序
- python thrift 實(shí)現(xiàn) 單端口多服務(wù)的過(guò)程
- python3.7通過(guò)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ù)端通信的使用示例
相關(guān)文章
python opencv根據(jù)顏色進(jìn)行目標(biāo)檢測(cè)的方法示例
這篇文章主要介紹了python opencv根據(jù)顏色進(jìn)行目標(biāo)檢測(cè)的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01Python深入學(xué)習(xí)之對(duì)象的屬性
這篇文章主要介紹了Python深入學(xué)習(xí)之對(duì)象的屬性,本文從較深的層次講解對(duì)象屬性的內(nèi)部運(yùn)行方式,需要的朋友可以參考下2014-08-08Python 如何實(shí)現(xiàn)文件自動(dòng)去重
這篇文章主要介紹了Python 實(shí)現(xiàn)文件自動(dòng)去重操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06對(duì)Python3.x版本print函數(shù)左右對(duì)齊詳解
今天小編就為大家分享一篇對(duì)Python3.x版本print函數(shù)左右對(duì)齊詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12如何使用Python實(shí)現(xiàn)名片管理系統(tǒng)
這篇文章主要介紹了如何使用Python實(shí)現(xiàn)名片管理系統(tǒng),文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下,希望對(duì)你的學(xué)習(xí)又是幫助2022-08-08Python使用sklearn庫(kù)實(shí)現(xiàn)的各種分類算法簡(jiǎn)單應(yīng)用小結(jié)
這篇文章主要介紹了Python使用sklearn庫(kù)實(shí)現(xiàn)的各種分類算法,結(jié)合實(shí)例形式分析了Python使用sklearn庫(kù)實(shí)現(xiàn)的KNN、SVM、LR、決策樹(shù)、隨機(jī)森林等算法實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-07-07Python數(shù)據(jù)分析numpy的Nan和Inf使用注意點(diǎn)詳解
這篇文章主要為大家介紹了Python數(shù)據(jù)分析numpy的Nan和Inf使用注意點(diǎn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08