Python Thrift sample <转> __python

Source: Internet
Author: User

Turn from: http://tkang.blogspot.com/2010/07/thrift-server-client-in-python.html

When writing Python thrift code, you need to first install thrift module

$ setup.py CD thrift-root/lib/py/
$ sudo python install

Here's a python example Helloworld.thrift

Const string Hello_in_korean = "An-nyoung-ha-se-yo"

const string hello_in_french = "bonjour!"

Const string Hello_in_japanese = "konichiwa!"

Service HelloWorld {
 void ping (),
 string SayHello (),
 string saymsg (1:string msg)
}


Production Code

$ thrift-r--gen py Helloworld.thrift

Writing server pythonserver.py

#!/usr/bin/env python import sys sys.path.append ('./gen-py ') from HelloWorld import HelloWorld from Helloworld.ttypes I Mport * from Thrift.transport import tsocket to Thrift.transport import ttransport from Thrift.protocol import Tbinaryp Rotocol from thrift.server import tserver import Socket class Helloworldhandler:def __init__ (self): Self.log = { def ping (self): print "ping ()" Def SayHello (self): print "SayHello ()" Return "Say hello from" + Socke T.gethostbyname (Socket.gethostname ()) def saymsg (self, msg): Print "saymsg (" + msg + ")" return "say" + msg + "From" + Socket.gethostbyname (Socket.gethostname ()) handler = HelloWorldHandler () processor = Helloworld.processor (Han Dler) transport = Tsocket.tserversocket (' 127.0.0.1 ', 30303) tfactory = Ttransport.tbufferedtransportfactory () pfactory

= Tbinaryprotocol.tbinaryprotocolfactory () Server = Tserver.tsimpleserver (processor, transport, tfactory, Pfactory) Print "Starting Python server ..."
Server.serve () print "done!"
 

Writing client pythonclient.py

#!/usr/bin/env python

import sys
sys.path.append ('./gen-py ') from

HelloWorld import HelloWorld
from Helloworld.ttypes Import * from
helloworld.constants import * to

Thrift import Thrift
from Thrift.transport import tsocket from
thrift.transport import ttransport from
thrift.protocol Import Tbinaryprotocol

try:
  # make socket
  transport = tsocket.tsocket (' 127.0.0.1 ', 30303)

  # buffering is Critical. Raw sockets are very slow
  transport = Ttransport.tbufferedtransport (transport)

  # Wrap in a protocol
  protocol = Tbinaryprotocol.tbinaryprotocol (transport)

  # Create A client to use the Protocol encoder
  client = Hel Loworld.client (Protocol)

  # connect!
  Transport.open ()

  client.ping ()
  print "ping ()"

  msg = Client.sayhello ()
  print msg
  msg = Client.saymsg (Hello_in_korean)
  print msg

  transport.close ()

except Thrift.texception, TX:
  print '%s '% (tx.message)

Running programs

 $ python pythonserver.py $ python pythonclient.py 

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.