Thrift Python Client: Quick and Dirty

Write better Python code with the latest version of Programming Python by O’Reilly


Thrift allows you to build standard services across multiple languages
The project was developed by Facebook and is now in the Apache Incubator

Get all the details here: http://incubator.apache.org/thrift/

I had to integrate with another application which was serving Thrift, and documentation is scarce, so here is how I built a Thrift client with Python v2.4 on CentOS 5.4 32-bit…

Lets refer to the Thrift service as ‘SomeApp’

Connect to your server and perform the following:

At this point, your ‘thrift’ binary should be sitting happily on your filesystem. Take it for a spin by executing ‘thrift’.

I had to do 1 more step to get the thrift python modules installed. Within the source you downloaded, navigate to this directory ‘thrift/lib/py’ and execute ‘python setup.py install’. Now you should be good to go.

Lets generate the Python modules using the Thrift ‘.service’ file which should be provided from the Thrift server.

Move the auto-generated code to whatever directory you want

I used ‘/opt/tools/SomeApp/‘

Here is a super simple example of utilizing the Thrift service in Python:

The example above just uses plain-text http and ideally you want SSL encryption. Currently I build the SSL session with Socat and point the Python code to the local host/port. Look for my next post which has instructions for this. I’ll try to get a tutorial together for building the Thrift server and enabling SSL in the client code.