Build a Simple Python Thrift Server and Client: Step‑by‑Step Guide
This tutorial walks you through installing Thrift, creating the interface file, and writing both server and client programs in Python to demonstrate a basic cross‑language RPC service.
This article briefly introduces how to build a simple test program with Python using Thrift.
1. Introduction
Thrift is a software framework for developing scalable cross‑language services. It combines a powerful software stack and code generation engine to enable seamless, efficient services among languages such as C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, and OCaml.
2. Installation
1. Download address
http://www.apache.org/dyn/closer.cgi?path=/thrift/0.9.2/thrift-0.9.2.tar.gz2. Install
[root@localhost ~]# yum -y groupinstall "Development Tools"
[root@localhost ~]# yum -y install libevent-devel zlib-devel openssl-devel autoconf automake
[root@localhost ~]# wget http://ftp.gnu.org/gnu/bison/bison-2.5.1.tar.gz
[root@localhost ~]# tar xf bison-2.5.1.tar.gz
[root@localhost ~]# cd bison-2.5.1
[root@localhost ~]# ./configure --prefix=/usr
[root@localhost ~]# make
[root@localhost ~]# make install
[root@localhost ~]# tar xf thrift-0.9.2.tar.gz
[root@localhost ~]# cd thrift-0.9.2
[root@localhost thrift-0.9.2]# ./configure -with-lua=no3. Install Python plugin
pip install thrift3. Prepare Server Side
1. Edit interface file helloworld.thrift :
#!/usr/bin/env python
import socket
import sys
sys.path.append('./gen-py')
from helloworld import HelloWorld
from helloworld.ttypes import *
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
class HelloWorldHandler:
def ping(self):
return "pong"
def say(self, msg):
ret = "Received: " + msg
print ret
return ret
# Create server
handler = HelloWorldHandler()
processor = HelloWorld.Processor(handler)
transport = TSocket.TServerSocket("localhost", 9090)
# Transport layer
tfactory = TTransport.TBufferedTransportFactory()
# Protocol
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
# Server instance
server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
print "Starting thrift server in python..."
server.serve()
print "done!"4. Prepare Client Side
#!/usr/bin/env python
import sys
sys.path.append('./gen-py')
from helloworld import HelloWorld
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
try:
# Create socket
transport = TSocket.TSocket('localhost', 9090)
# Transport layer (must match server)
transport = TTransport.TBufferedTransport(transport)
# Protocol (must match server)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
# Client
client = HelloWorld.Client(protocol)
transport.open()
print "client - ping"
print "server - " + client.ping()
print "client - say"
msg = client.say("Hello!")
print "server - " + msg
transport.close()
except Thrift.TException, ex:
print "%s" % (ex.message)PS. This is a small example of a Thrift server and client. Thrift shines when multiple languages are involved; if you work in a single language, its benefits are limited. With Thrift you can call functions from other languages or expose services for them, making cross‑language development convenient.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
