1. 程式人生 > >基於thrift的java和python分別作為客戶端和服務端的調用實現

基於thrift的java和python分別作為客戶端和服務端的調用實現

Coding except arr pes com ssa utf-8 encoding 中文亂碼

前面已經實現了純java的thrift的實現。

現在實現實現一下python作為客戶端和服務端的thrift的調用

1.python作為客戶端,java作為服務端

java服務端代碼參考前面寫的博客

客戶端python的準備:

1.使用mac下的PyCharm,專業的python開發工具

2.生成python代碼

thrift --gen py thrift/data.thrift

3.在mac下安裝thrift的python依賴

sudo python setup.py install 安裝thrift的python依賴

4.查看安裝依賴的路徑

安裝的路徑:/Library/Python/2.7/site-packages

python客戶端代碼 :

# -*- coding:utf-8 -*-
__author__ = 作者
//導入thrift生成的業務代碼
from py.com.fubin.netty import PersonService
from py.com.fubin.netty import ttypes
//導入thrift框架的代碼
from thrift import Thrift
from thrift.transport import  TSocket
from thrift.transport import  TTransport
from thrift.protocol import
TCompactProtocol
//解決中文亂碼問題
import sys reload(sys) sys.setdefaultencoding(utf-8 ) try: tSocket = TSocket.TSocket("localhost",8899) tSocket.setTimeout(600) transport = TTransport.TFramedTransport(tSocket) protocol = TCompactProtocol.TCompactProtocol(transport) client = PersonService.Client(protocol) transport.open() person
= client.getPersonByUsername("zhangsan") print person.username print person.age print person.married print ======================= newPerson = ttypes.Person() newPerson.username = "付彬" newPerson.age = 11 newPerson.married = True client.savePerson(newPerson) transport.close() except Thrift.TException , tx: print %s % tx.message

中文亂碼問題:導入系統庫

import sys
reload(sys)
sys.setdefaultencoding(utf-8 )

python作為服務端

1.編寫業務接口類

# -*- coding:utf-8 -*-
__author__ = 作者

from py.com.fubin.netty import ttypes

class PersonServiceImpl :

    def getPersonByUsername(self,username):
        print  "got client param :" +username

        person = ttypes.Person()
        person.username = username
        person.age = 112
        person.married = True

        return person

    def savePerson(self ,person):
        print  "got client param : "

        print person.username
        print person.age
        print person.married

編寫服務端初始化類:

# -*- coding:utf-8 -*-
__author__ = 作者

from py.com.fubin.netty import PersonService
from PythonServiceImpl import  PersonServiceImpl
from py.com.fubin.netty import ttypes

from thrift import Thrift
from thrift.transport import  TSocket
from thrift.transport import  TTransport
from thrift.protocol import   TCompactProtocol
from thrift.server import  TServer

import sys
reload(sys)

sys.setdefaultencoding(utf-8 )

try:

    personServiceHandler = PersonServiceImpl()
    processor = PersonService.Processor(personServiceHandler)

    serverSocket = TSocket.TServerSocket(port=8899)
    transportFactory = TTransport.TFramedTransportFactory()
    protocolFactory = TCompactProtocol.TCompactProtocolFactory()

    server = TServer.TSimpleServer(processor,serverSocket,transportFactory,protocolFactory)
    server.serve()

except Thrift.TException , tx:
    print %s % tx.message

到這裏,一個簡單的python實現的thrift調用例子就完成了。

不積跬步,無以至千裏,每天進步一點點。

基於thrift的java和python分別作為客戶端和服務端的調用實現