thrift-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From HIRANO Satoshi <hiran...@aist.go.jp>
Subject Module to integrate thrift with Python web framework (WSGI, Django)
Date Thu, 17 Nov 2011 00:19:55 GMT
I think IOStreamTransport.py should be included in the package. Does anybody test this?  If
OK, I will write it to JIRA.

This module realizes thrift to run on WSGI frameworks. Usage is described in the comment.

--------- thrift/transport/IOStreamTransport.py -------------

#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

# This transport module is compatible with WSGI frameworks. (e.g. webapp on Google App Engine)
# For more detail visit http://symfoware.blog68.fc2.com/blog-entry-320.html
#
# Usage: Copy the following code fragment into your code. 
#
#from thrift.protocol import TBinaryProtocol
#from thrift.transport import TIOStreamTransport
#
# Add generated files by thrift compiler
#import sys
#sys.path.append('./gen-py')
#
#from sample import MyService
#from sample.ttypes import *
#
#class MyServiceHandler:
#    def ping(self, a):
#        return a
#
#class MainHandler(webapp.RequestHandler):
#    
#    def post(self):
#        handler = MyServiceHandler()
#        processor = MyService.Processor(handler)
#                
#        self.response.headers['Content-Type'] = 'application/x-thrift'
#        
#        # user binary protocol through WSGI
#        input_stream = self.request.environ['wsgi.input']
#        output_stream = self.response.out
#        transport = TIOStreamTransport.TIOStreamTransport(input_stream, output_stream)
#        
#        protocol = TBinaryProtocol.TBinaryProtocol(transport, True, True)
#        transport.open()
#        processor.process(protocol, protocol)
#        transport.close()
#
#def main():
#    application = webapp.WSGIApplication([('/', MainHandler)],
#                                     debug=True)
#    wsgiref.handlers.CGIHandler().run(application)
#    # run_wsgi_app(application) # use this on GAE instead of above.
#
#if __name__ == '__main__':
#    main()

from thrift.transport.TTransport import TTransportBase

class TIOStreamTransport(TTransportBase):

    def __init__(self, input_stream, output_stream):
        self.input_stream = input_stream
        self.output_stream = output_stream
        
    def isOpen(self):
        return True

    def close(self):
        pass

    def read(self, sz):
        return self.input_stream.read(sz)

    def write(self, buf):
        self.output_stream.write(buf)

    def flush(self):
        pass

@@@@@@@@@@@@@@  Life is beautiful and full of surprises.

HIRANO Satoshi, Ph.D.         <hirano-s@aist.go.jp>
AIST: National Institute of Advanced Industrial Science and Technology


Mime
View raw message