『高級篇』docker之Python開發信息服務(11)

>原創文章,歡迎轉載。轉載請註明:轉載自IT人故事會,謝謝!
>原文鏈接地址:『高級篇』docker之Python開發信息服務(11)

信息服務準備用python來寫,在現有的idea中添加python的模塊。源碼:https://github.com/limingios/msA-docker

idea安裝python插件

安裝後重新idea。

『高級篇』docker之Python開發信息服務(11)

『高級篇』docker之Python開發信息服務(11)

『高級篇』docker之Python開發信息服務(11)

安裝python模塊

『高級篇』docker之Python開發信息服務(11)

『高級篇』docker之Python開發信息服務(11)

安裝thrift的pyhon插件

『高級篇』docker之Python開發信息服務(11)

『高級篇』docker之Python開發信息服務(11)

開始我用idea寫python,下載個插件都費勁,我換成了pycharm來寫美滋滋

  • 編輯Python的服務代碼
# coding: utf-8
from message.api import MessageService
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer

class MessageServiceHandler:

    def sendMobileMessage(self, mobile, message):
        print ("sendMobileMessage, mobile:"+mobile+", message:"+message)
        return True

    def sendEmailMessage(self, email, message):
        print ("sendEmailMessage, email:"+email+", message:"+message)
        return True

if __name__ == '__main__':
    handler = MessageServiceHandler()
    processor = MessageService.Processor(handler)
    transport = TSocket.TServerSocket(None, "9090")
    tfactory = TTransport.TFramedTransportFactory()
    pfactory = TBinaryProtocol.TBinaryProtocolFactory()

    server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
    print ("python thrift server start")
    server.serve()
    print ("python thrift server exit")

『高級篇』docker之Python開發信息服務(11)

  • 查看端口已經啓動

『高級篇』docker之Python開發信息服務(11)

  • 生成對應java 和python的命令

    都是根據thrift文件,生成對應的上級目錄

    thrift --gen py -out ../ message.thrift
    thrift --gen java -out ../ message.thrift

PS:thrift的開發流程是: 先定義thrift的文件,然後通過命令生成對應的python代碼。通過實現定義的thrift方法,來完成thrift的調用。

『高級篇』docker之Python開發信息服務(11)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章