Thrift-based Java and Python are implemented as client and server-side invocations respectively

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.