Python&Thrift--Server&Client

来源:互联网 发布:呼叫中心软件 编辑:程序博客网 时间:2024/06/15 23:10

thrift目前只支持python2.6+,但不支持3.XX版本。

thrift下载:http://thrift.apache.org/

安装thrift:

Java代码  收藏代码
  1. ./configure  
  2. make  
  3. make install  

 安装python的thrift组件

Java代码  收藏代码
  1. cd /usr/local/thrift-0.9.1/lib/py  
  2. python setup.py install  

 

thrift模版文件PythonService.thrift

Java代码  收藏代码
  1. service PythonService{    
  2.     string get(1:i32 id)    
  3.     i32 remove(1:i32 id)    
  4. }    

 

生成python文件

Java代码  收藏代码
  1. thrift --gen py PythonService.thrift  

将gen-py/PythonService路径下的全部文件拷贝到自己的项目路径下,比如PythonThrift\servicePy

 

server端:

PythonThrift\server\PythonServiceServer.py

Java代码  收藏代码
  1. # coding=utf-8  
  2. '''  
  3. Created on 2013-9-22  
  4.   
  5. @author: hanqunfeng  
  6. '''  
  7.   
  8. import sys  
  9. sys.path.append('../') #导入上下文环境  
  10.   
  11. from servicePy import  PythonService  
  12. from thrift import Thrift  
  13. from thrift.transport import TSocket  
  14. from thrift.transport import TTransport  
  15. from thrift.protocol import TBinaryProtocol  
  16. from thrift.protocol import TCompactProtocol  
  17. from thrift.server import TServer  
  18.   
  19.   
  20. import socket  
  21. # 实现类  
  22. class PythonServiceServer:  
  23.      def get(self, id):  
  24.          print socket.gethostbyname(socket.gethostname())  
  25.          return "get=="+str(id)  
  26.        
  27.      def remove(self, id):  
  28.          print socket.gethostbyname(socket.gethostname())  
  29.          return id  
  30.        
  31. handler = PythonServiceServer()  
  32. # 注册实现类  
  33. processor = PythonService.Processor(handler)  
  34. transport = TSocket.TServerSocket('localhost',30303)  
  35. tfactory = TTransport.TBufferedTransportFactory()  
  36. # pfactory = TBinaryProtocol.TBinaryProtocolFactory()  
  37. pfactory = TCompactProtocol.TCompactProtocolFactory()  
  38.   
  39. server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)  
  40.   
  41. print "Starting python server..."  
  42. server.serve()  
  43. print "done!"      

 

client端:

PythonThrift\client\PythonServiceClient.py

Java代码  收藏代码
  1. # coding=utf-8  
  2. '''  
  3. Created on 2013-9-22  
  4.   
  5. @author: hanqunfeng  
  6. '''  
  7.   
  8. import sys  
  9. sys.path.append('../') #导入上下文环境  
  10.   
  11. from servicePy import  PythonService  
  12. from thrift import Thrift  
  13. from thrift.transport import TSocket  
  14. from thrift.transport import TTransport  
  15. from thrift.protocol import TBinaryProtocol  
  16. from thrift.protocol import TCompactProtocol  
  17.   
  18. def pythonServerExe():  
  19.     try:  
  20.         transport = TSocket.TSocket('localhost'30303)   
  21.         transport = TTransport.TBufferedTransport(transport)  
  22.         # protocol = TBinaryProtocol.TBinaryProtocol(transport)  
  23.         protocol = TCompactProtocol.TCompactProtocol(transport)  
  24.         client = PythonService.Client(protocol)  
  25.         transport.open()  
  26.         print "The return value is : "   
  27.         print client.remove(12)  
  28.         print client.get(100)  
  29.         print "............"  
  30.         transport.close()  
  31.     except Thrift.TException, tx:  
  32.         print '%s' % (tx.message)  
  33.           
  34.           
  35. if __name__ == '__main__':  
  36.     pythonServerExe()  

 

0 0
原创粉丝点击