Python thrift builds a server and client test program, pythonthrift

Source: Internet
Author: User

Python thrift builds a server and client test program, pythonthrift

This article describes how to build a simple test program for the server and client through python.

I. Introduction

Thrift is a software framework used to develop scalable and cross-language services. It integrates powerful software stacks and code generation engines to build in C ++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C #, Cocoa, javaScript, Node. javascript, Smalltalk, and OCaml programming languages are seamlessly integrated and efficient services.

Ii. Installation

1.

 http://www.apache.org/dyn/closer.cgi?path=/thrift/0.9.2/thrift-0.9.2.tar.gz

2. Installation

 [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 python plug-in

pip install thrift

3. Prepare the server

1. Edit the interface file helloworld. thrift:

#! /Usr/bin/env python import socketimport syssys. 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 = "pinned ed:" + msg print ret Return ret # create server handler = HelloWorldHandler () processor = HelloWorld. processor (handler) # listener port transport = TSocket. TServerSocket ("localhost", 9090) # select transport layer tfactory = TTransport. TBufferedTransportFactory () # select transmission protocol pfactory = TBinaryProtocol. TBinaryProtocolFactory () # create server = TServer. TSimpleServer (processor, transport, tfactory, pfactory) print "Starting thrift server in python... "server. serve () print "Done! "

4. Prepare the client

#! /Usr/bin/env pythonimport syssys. path. append ('. /gen-py ') from helloworld import HelloWorld # introduce the client class from thrift import Thrift from thrift. transport import TSocketfrom thrift. transport import TTransportfrom thrift. protocol import TBinaryProtocoltry: # create socket transport = TSocket. TSocket ('localhost', 9090) # select the transport layer, which must be consistent with the settings on the server. transport = TTransport. TBufferedTransport (transport) # select the transmission protocol, which must be consistent with the server; otherwise, the server cannot Communication protocol = TBinaryProtocol. TBinaryProtocol (transport) # create client = HelloWorld. client (protocol) transport. open () print "client-ping" print "server-" + client. ping () print "client-say" msg = client. say ("Hello! ") Print" server-"+ msg # disable transmission transport. close () # Catch exceptions when T Thrift. TException, ex: print" % s "% (ex. message)

PS. This is a small case of thrift's server and client implementation. Generally, thrift is used only when multiple languages are used for joint development. If it is a language, thrift will be useless. During multi-language development, we can get thrift files from other languages and directly use our python as the client to call the functions in thrift, or we can provide thrift server files for other languages to call. It is very convenient in general. I hope the above example will help you understand thrift's simple application!

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.