Python Thrift Simple Example __python

Source: Internet
Author: User

Helloservice.thrift

Service HelloService {
    void SayHello ()
	string getData (1:string input)
	
}

Thrift-gen py Helloservice.thrift

You can generate gen-py

server.py

#!/usr/bin/env Python # Licensed to the Apache Software Foundation (ASF) under a # or more contributor license Ents. The NOTICE file # distributed with this work for additional information # regarding copyright. The ASF licenses this file # to you under the Apache License, Version 2.0 (the # "License"); You are not to use this file except in compliance # with the License.  You may obtain a copy of the License in # # http://www.apache.org/licenses/LICENSE-2.0 # unless required by applicable Agreed to in writing, # Software distributed under the License was distributed on a # ' as is ' basis, without Warra Nties or CONDITIONS of any # KIND, either express OR implied.
The License for the # specific language governing permissions and limitations # under the License.  # import sys, glob from helloservice import HelloService to helloservice.ttypes Import * from Thrift.transport Import Tsocket from Thrift.transport import ttransport from Thrift.protocol imPort Tbinaryprotocol from thrift.server import Tserver class Helloservicehandler:def __init__ (self): Self.log = {} def func1 (self): print ' func1 () ' Def SayHello (self): print ' SayHello ' def getData (self, input): retur


n input+ ' from server 1024 '; Handler = Helloservicehandler () processor = Helloservice.processor (handler) transport = Tsocket.tserversocket (port= 9090) Tfactory = Ttransport.tbufferedtransportfactory () pfactory = tbinaryprotocol.tbinaryprotocolfactory () Server = Tserver.tsimpleserver (processor, transport, tfactory, pfactory) # You could does one of these for a multithreaded server #s Erver = Tserver.tthreadedserver (processor, transport, tfactory, pfactory) #server = Tserver.tthreadpoolserver (
 Processor, transport, tfactory, pfactory) print ' Starting the server ... ' Server.serve () print ' done. '


client.py

#!/usr/bin/env Python # Licensed to the Apache Software Foundation (ASF) under a # or more contributor license Ents. The NOTICE file # distributed with this work for additional information # regarding copyright. The ASF licenses this file # to you under the Apache License, Version 2.0 (the # "License"); You are not to use this file except in compliance # with the License.  You may obtain a copy of the License in # # http://www.apache.org/licenses/LICENSE-2.0 # unless required by applicable Agreed to in writing, # Software distributed under the License was distributed on a # ' as is ' basis, without Warra Nties or CONDITIONS of any # KIND, either express OR implied.
The License for the # specific language governing permissions and limitations # under the License. # import sys, glob from helloservice import HelloService to helloservice.ttypes Import * from thrift Import Thrift F Rom thrift.transport import tsocket from thrift.transport import TTRANSPORT from Thrift.protocol Import Tbinaryprotocol try: # make Socket transport = tsocket.tsocket (' localhost ', 9090) # buffering is critical. Raw sockets are very slow transport = Ttransport.tbufferedtransport (transport) # Wrap in a protocol protocol = Tbin Aryprotocol.tbinaryprotocol (Transport) # Create a client to use the Protocol encoder client = Helloservice.client (pro
  Tocol) # connect!
  Transport.open () Client.sayhello ();
  print ' ping () ' Print (Client.getdata ("Client Access") # close!
 Transport.close () except Thrift.texception, Tx:print '%s '% (tx.message)




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.