Thrift python
注:
thrift
不支持python 3
thrift -r --gen py app.thrift
使用 conda
分配 python 2.7
环境,用 pip
安装 thrift
包: pip install thrift
app.thrift:
service Light {
string hello(1:string name)
}
server.py:
import sys sys.path.append('gen-py') from app import Light from app.ttypes import * from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol from thrift.server import TServer class Handler: def __init__(self): self.log = {} def hello(self, name): return name if __name__ == '__main__': handler = Handler() processor = Light.Processor(handler) transport = TSocket.TServerSocket(port = 9090) tfactory = TTransport.TBufferedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() server = TServer.TSimpleServer(processor, transport, tfactory, pfactory) print "Starting thrift server in python..." server.serve()
Client.py:
import sys sys.path.append('gen-py') from app import Light from app.ttypes import * from thrift import Thrift from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol transport = TSocket.TSocket() transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = Light.Client(protocol) transport.open() print(client.hello('kevin')) transport.close()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论