2009
11.27

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:

# upgrade/install packages (your setup may vary)
yum upgrade autoconf
yum upgrade automake
yum install gcc-c++ automake libtool
yum install boost boost-devel
yum install python-devel
yum install perl-Bit-Vector perl-Class-Accessor

# Get the Thrift code
wget -O thrift.tgz "http://gitweb.thrift-rpc.org/?p=thrift.git;a=snapshot;h=HEAD;sf=tgz"
tar -xzvf thrift.tgz
cd thrift

# build Thrift
./bootstrap.sh
./configure --without-csharp
make
make install

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.

thrift --gen py SomeApp-service.thrift

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:

#!/usr/bin/env python
import sys
sys.path.append('/opt/tools/SomeApp/')
import SomeApp
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol

def main():

    # establish connection to SomeApp
    try:
        transport = TSocket.TSocket('service.SomeApp.com', '80')
        transport = TTransport.TBufferedTransport(transport)
        protocol = TBinaryProtocol.TBinaryProtocol(transport)
        SomeApp_link = SomeApp.Client(protocol)
        transport.open()
    except:
		error_msg = 'Connect to SomeApp failed'
        logError(error_msg)

	# utilize the Thrift service (call method 'recordUserName')
	recordUserName = SomeApp_link.recordUserName('joe')

if __name__ == "__main__":
    main()

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.

2 comments so far

Add Your Comment
  1. This is the best documentation Ive ever seen

  2. Dwight Shrute you are my hero