The implementation of pure Java Thrift has been implemented in the preceding.
Now implement the call of Python as a client and server thrift
1.python as a client, Java as a server
Java service-side code reference the blog that was written earlier
Preparation of client Python:
1. Using Pycharm under Mac, professional Python development tools
2. Generate Python Code
Thrift--gen PY Thrift/data.thrift
3. Install Thrift Python dependency on Mac
sudo python setup.py install thrift python Dependency
4. View the path where the installation is dependent
Installed path:/library/python/2.7/site-packages
Python Client code:
#-*-coding:utf-8-*-__author__='author'Import the business code generated by the thrift fromPy.com.fubin.nettyImportPersonservice fromPy.com.fubin.nettyImportttypes//Importing the Thrift Framework Code fromThriftImportThrift fromThrift.transportImportTsocket fromThrift.transportImportTtransport fromThrift.protocolImportTcompactprotocol
Solve Chinese garbled problemImportsysreload (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") PrintPerson.usernamePrintPerson.agePrintperson.marriedPrint '======================='Newperson=Ttypes. Person () Newperson.username="Fubin"Newperson.age= 11newperson.married=True Client.saveperson (Newperson) transport.close ()exceptThrift.texception, TX:Print '%s'% Tx.message
Chinese garbled problem: Import System Library
Import sysreload (SYS) sys.setdefaultencoding (")
Python as a service side
1. Writing the Business interface class
#-*-coding:utf-8-*-__author__='author' fromPy.com.fubin.nettyImportTtypesclassPersonserviceimpl:defGetpersonbyusername (self,username):Print "got client param:"+username Person=Ttypes. Person () Person.username=username Person.age= 112person.married=Truereturn PersondefSaveperson (self, person):Print "got client param:" PrintPerson.usernamePrintPerson.agePrintPerson.married
Write the service-side initialization class:
#-*-coding:utf-8-*-__author__='author' fromPy.com.fubin.nettyImportPersonservice fromPythonserviceimplImportPersonserviceimpl fromPy.com.fubin.nettyImportTtypes fromThriftImportThrift fromThrift.transportImportTsocket fromThrift.transportImportTtransport fromThrift.protocolImportTcompactprotocol fromThrift.serverImportTserverImportsysreload (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 ()exceptThrift.texception, TX:Print '%s'% Tx.message
Here, a simple python implementation of the thrift invocation example is complete.
Do not accumulate kuibu, not even thousands of miles, daily progress a little.
Thrift-based Java and Python are implemented as client and server-side invocations respectively