佳木斯湛栽影视文化发展公司

主頁 > 知識(shí)庫 > python實(shí)現(xiàn)Thrift服務(wù)端的方法

python實(shí)現(xiàn)Thrift服務(wù)端的方法

熱門標(biāo)簽:百度AI接口 客戶服務(wù) Win7旗艦版 硅谷的囚徒呼叫中心 呼叫中心市場(chǎng)需求 企業(yè)做大做強(qiáng) 電話運(yùn)營中心 語音系統(tǒng)

近期在項(xiàng)目中存在跨編程語言協(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庫:pip install thrift

編寫.thrift文件

.thrift文件定義了Thrift服務(wù)端和Thrift客戶端的通信接口,在該文件中定義的接口需由服務(wù)端實(shí)現(xiàn),并可被客戶端調(diào)用。Thrift編譯器會(huì)調(diào)用.thrift文件生成不同語言的thrift代碼,用于之后實(shí)現(xiàn)thrift服務(wù)端或thrift客戶端。

.thrift文件的編寫規(guī)則可參考Thrift白皮書。下面將以demo.thrift文件舉例

service DemoService{
    string ping(1:string param)
    mapi32,string> get_int_string_mapping_result(1:i32 key, 2:string value)
    bool get_bool_result()
}

生成python對(duì)應(yīng)的thrift代碼

使用以下命令可以生成不同語言的thrift代碼:

thrift --gen language> Thrift filename>

 通過thrift-0.9.3.exe --gen py demo.thrift 命令生成python版本的thrift文件,文件夾為gen-py,如下所示:

編寫服務(wù)端

編寫服務(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)聽端口
    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()

編寫客戶端用于測(cè)試

編寫客戶端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()

編寫完成后,整個(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)試過程中,也許需要修改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ù)的過程
  • 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ù)端通信的使用示例

標(biāo)簽:喀什 山西 海南 崇左 濟(jì)南 山西 長(zhǎng)沙 安康

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python實(shí)現(xiàn)Thrift服務(wù)端的方法》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    山丹县| 三门峡市| 乌审旗| 乌拉特后旗| 邵阳县| 蒙自县| 会同县| 青冈县| 怀仁县| 琼海市| 金山区| 巨鹿县| 赫章县| 资溪县| 汉阴县| 汉川市| 柞水县| 灯塔市| 蛟河市| 焦作市| 潼南县| 云南省| 迁西县| 磴口县| 调兵山市| 卢氏县| 凤台县| 桦南县| 合作市| 台山市| 安泽县| 新源县| 利川市| 杭锦后旗| 平泉县| 永川市| 长治县| 永仁县| 金平| 双城市| 濮阳县|