Skip to content

Instantly share code, notes, and snippets.

@beanyoung
Last active October 8, 2020 15:12
Show Gist options
  • Save beanyoung/8318363 to your computer and use it in GitHub Desktop.
Save beanyoung/8318363 to your computer and use it in GitHub Desktop.
thrift server based on flask
from flask import Flask, request, make_response
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
from thrift.transport import TTransport
from FooService import FooService
from foo_service_handler import FooServiceHandler
foo_handler = FooServiceHandler()
foo_processor = FooService.Processor(foo_handler)
foo_pfactory = TBinaryProtocol.TBinaryProtocolFactory()
foo_server = TServer.TServer(
foo_processor,
None, None, None,
foo_pfactory,
foo_pfactory)
app = Flask(__name__)
app.config.from_object('config')
@app.route('/', methods=['POST'])
def profile_service_api():
itrans = TTransport.TMemoryBuffer(request.data)
otrans = TTransport.TMemoryBuffer()
iprot = foo_server.inputProtocolFactory.getProtocol(itrans)
oprot = foo_server.outputProtocolFactory.getProtocol(otrans)
foo_server.processor.process(iprot, oprot)
return make_response(otrans.getvalue())
@petrabarus
Copy link

this is awesome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment