Thrift的服务器和客户端Python案例

举报
Lansonli 发表于 2021/09/27 23:17:22 2021/09/27
【摘要】 服务器 Thrift提供的常见服务端类型有一下几种: thrift.server.TServer.TSimpleServer 单线程服务器thrift.server.TServer.TThreadedServer 多线程服务器thrift.server.TServer.TThreadPoolServer&nbsp...

服务器

Thrift提供的常见服务端类型有一下几种:

  • thrift.server.TServer.TSimpleServer 单线程服务器
  • thrift.server.TServer.TThreadedServer 多线程服务器
  • thrift.server.TServer.TThreadPoolServer 线程池服务器
  • thrift.server.TServer.TForkingServer 多进程服务器

我们以线程池服务器为例,其他方式相同。


  
  1. import sys
  2. sys.path.append('gen-py') # 增加生成代码的查找包路径
  3. from calculate import Calculate
  4. from base.ttypes import InvalidOperation, Operation
  5. from thrift.transport import TSocket
  6. from thrift.transport import TTransport
  7. from thrift.protocol import TCompactProtocol
  8. from thrift.server import TServer
  9. class CalculateHandler(Calculate.Iface):
  10. """被调用方法的具体实现"""
  11. def ping(self):
  12. print('ping()')
  13. def divide(self, num1, num2):
  14. if num2 == 0:
  15. raise InvalidOperation(0, 'Cannot divide by 0')
  16. return num1 / num2
  17. def calculate(self, work):
  18. if work.op == Operation.ADD:
  19. val = work.num1 + work.num2
  20. elif work.op == Operation.SUBTRACT:
  21. val = work.num1 - work.num2
  22. elif work.op == Operation.MULTIPLY:
  23. val = work.num1 * work.num2
  24. else:
  25. raise InvalidOperation(work.op, 'Invalid operation')
  26. return val
  27. if __name__ == '__main__':
  28. handler = CalculateHandler()
  29. processor = Calculate.Processor(handler)
  30. transport = TSocket.TServerSocket(host='127.0.0.1', port=8888)
  31. tfactory = TTransport.TBufferedTransportFactory()
  32. pfactory = TCompactProtocol.TCompactProtocolFactory()
  33. server = TServer.TThreadPoolServer(processor, transport, tfactory, pfactory)
  34. server.serve()
  35. print('服务器已启动')

客户端


  
  1. import sys
  2. sys.path.append('gen-py')
  3. from calculate import Calculate
  4. from base.ttypes import InvalidOperation, Operation, Work
  5. from thrift.transport import TSocket
  6. from thrift.transport import TTransport
  7. from thrift.protocol import TCompactProtocol
  8. def main():
  9. transport = TSocket.TSocket('127.0.0.1', 8888)
  10. # 使用缓存传输
  11. transport = TTransport.TBufferedTransport(transport)
  12. # 使用压缩的二进制消息协议
  13. protocol = TCompactProtocol.TCompactProtocol(transport)
  14. client = Calculate.Client(protocol)
  15. # 连接
  16. transport.open()
  17. client.ping()
  18. print('ping()')
  19. result = client.divide(100, 50)
  20. print('100/50={}'.format(result))
  21. try:
  22. result = client.divide(100, 0)
  23. except InvalidOperation as e:
  24. print(e.why)
  25. work = Work(1, 2, Operation.ADD)
  26. result = client.calculate(work)
  27. print('1+2={}'.format(result))
  28. work = Work(15, 10, Operation.SUBTRACT)
  29. result = client.calculate(work)
  30. print('15-10={}'.format(result))
  31. # 关闭
  32. transport.close()
  33. if __name__ == '__main__':
  34. main()

 

文章来源: lansonli.blog.csdn.net,作者:Lansonli,版权归原作者所有,如需转载,请联系作者。

原文链接:lansonli.blog.csdn.net/article/details/105304671

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。