Python&Thrift--Server&Client

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值