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)