Tuesday, July 13, 2010
Thrift Server-Client in Python
1. Write Thrift stub code
[tkang@neb005 thrift]$ vi helloworld.thrift
| 1 2 3 4 5 6 7 8 9 10 11 | conststringHELLO_IN_KOREAN = "an-nyoung-ha-se-yo"conststringHELLO_IN_FRENCH = "bonjour!"conststringHELLO_IN_JAPANESE = "konichiwa!"service HelloWorld {  voidping(),  i32 sayHello(),  i32 sayMsg(1:stringmsg)} | 
2. Generate python code
[tkang@neb005 thrift]$ thrift --gen py helloworld.thrift
Generated codes will be saved under "gen-py" directory.
[tkang@neb005 thrift]$ ls
gen-py helloworld.thrift
3. Fill in Server code
[tkang@neb005 thrift]$ mkdir py-impl
[tkang@neb005 thrift]$ cd py-impl
[tkang@neb005 py-impl]$ vi PythonServer.py
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | #!/usr/bin/env pythonimportsyssys.path.append('../gen-py')fromhelloworld importHelloWorldfromhelloworld.ttypes import*fromthrift.transport importTSocketfromthrift.transport importTTransportfromthrift.protocol importTBinaryProtocolfromthrift.server importTServerimportsocketclassHelloWorldHandler:  def__init__(self):    self.log ={}  defping(self):    print"ping()"  defsayHello(self):    print"sayHello()"    return"say hello from "+socket.gethostbyname(socket.gethostname())  defsayMsg(self, msg):    print"sayMsg("+msg +")"    return"say "+msg +" from "+socket.gethostbyname(socket.gethostname())handler =HelloWorldHandler()processor =HelloWorld.Processor(handler)transport =TSocket.TServerSocket(30303)tfactory =TTransport.TBufferedTransportFactory()pfactory =TBinaryProtocol.TBinaryProtocolFactory()server =TServer.TSimpleServer(processor, transport, tfactory, pfactory)print"Starting python server..."server.serve()print"done!" | 
4. Write Client code to connect to server
[tkang@neb005 py-impl]$ vi PythonClient.py
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | #!/usr/bin/env pythonimportsyssys.path.append('../gen-py')fromhelloworld importHelloWorldfromhelloworld.ttypes import*fromhelloworld.constants import*fromthrift importThriftfromthrift.transport importTSocketfromthrift.transport importTTransportfromthrift.protocol importTBinaryProtocoltry:  # Make socket  transport =TSocket.TSocket('localhost', 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 =HelloWorld.Client(protocol)  # Connect!  transport.open()  client.ping()  print"ping()"  msg =client.sayHello()  printmsg  msg =client.sayMsg(HELLO_IN_KOREAN)  printmsg  transport.close()exceptThrift.TException, tx:  print"%s"%(tx.message) | 
 
                    
                     
                    
                 
                    
                 
 
         
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号