python thrift builds server and client test programs
- 2020-07-21 08:54:10
- OfStack
This article describes how to build a simple test program for server and client through python.
1. Introduction
thrift is a software framework for the development of extensible, cross-language services. It combines powerful software stacks and code generation engines to build seamless, efficient services between C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node. js, Smalltalk, and OCaml.
2. Install
1. Download address
http://www.apache.org/dyn/closer.cgi?path=/thrift/0.9.2/thrift-0.9.2.tar.gz
2. 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=no
3. Install the python plug-in
pip install thrift
Prepare the server side
1. Edit interface file ES42en. 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 the server side
handler = HelloWorldHandler()
processor = HelloWorld.Processor(handler)
# Listen on port
transport = TSocket.TServerSocket("localhost", 9090)
# Select transport layer
tfactory = TTransport.TBufferedTransportFactory()
# Select transport protocol
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
# Create the server side
server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
print "Starting thrift server in python..."
server.serve()
print "done!"
4. Prepare the client
#!/usr/bin/env python
import sys
sys.path.append('./gen-py')
from helloworld import HelloWorld # Introduce the client class
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
try:
# To establish socket
transport = TSocket.TSocket('localhost', 9090)
# Select the transport layer, which you want to set up with the server 1 to
transport = TTransport.TBufferedTransport(transport)
# Select the transport protocol, which also needs to be maintained with the server 1 Send, otherwise no communication can be made
protocol = TBinaryProtocol.TBinaryProtocol(transport)
# Create client
client = HelloWorld.Client(protocol)
transport.open()
print "client - ping"
print "server - " + client.ping()
print "client - say"
msg = client.say("Hello!")
print "server - " + msg
# Close the transmission
transport.close()
# Catch exceptions
except Thrift.TException, ex:
print "%s" % (ex.message)
PS. This is a small example of thrift's server and client implementations. Generally, thrift is only used for joint development of multiple languages. If it is a single language, thrift is useless. In language development, the more we get other languages thrift file, you can directly use our python thrift as the client to call the function is ok, or we provide thrift server files for other language calls, in total is very convenient, hope that the example above can let everybody understand the simple application of thrift!