老师,Python 使用Thrift的问题

来源:3-8 开发用户EdgeService_C

菠萝吹雪gz

2018-06-25

环境:Python 3.6.2 Thrift 0.9.3

客户端代码:

from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from message.api import MessageService

from io import *
__HOST = 'localhost'
__PORT = 8800

b = StringIO()

tsocket = TSocket.TSocket(__HOST, __PORT)
transport = TTransport.TBufferedTransport(tsocket)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = MessageService.Client(protocol)
client.send_sendEmailMessage()

transport.open()


服务端代码:

from message.api import MessageService
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer


class MessageServiceHandler:
   def sendMobileMessage(self):
       print("sendMobileMessage")
       return True

   def sendEmailMessage(self):
       print("sendEmailMessage")
       return True


if __name__ == "__main__":
   handler = MessageServiceHandler()
   processor = MessageService.Processor(handler)
   transport = TSocket.TServerSocket("localhost", 8800)
   tfactory = TTransport.TBufferedTransportFactory()
   pfactory = TBinaryProtocol.TBinaryProtocolFactory()

   server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
   print("python thrift server start")
   server.serve()
   print("python thrift server exit")

客户端调用报错:

http://img.mukewang.com/szimg/5b307afc000198af10930391.jpg

写回答

1回答

刘果国

2018-06-26

看错误的意思是参数问题,需要一个string类型,你传了bytes类型。

thrift这块我们用的版本是python2.7和thrift0.10.0,如果用原有代码可能会有版本兼容问题哦

0
1
菠萝吹雪gz
谢谢老师的回答,问题我已经解决了,完成了c#和python3.6.2的互调
2018-06-26
共1条回复

Docker+Kubernetes(k8s)微服务容器化实践

从开发到编排,快速,完整,深入的掌握微服务

2608 学习 · 607 问题

查看课程