You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@thrift.apache.org by Li Lirong <le...@gmail.com> on 2011/11/15 06:24:22 UTC

Integrating thrift with Python web framework (Django)

Hi,

We are developing web application with Django (djangoproject.org).  Is
there any tutorials on exposing thrift web services within the Django
framework?

Best regards

Re: Integrating thrift with Python web framework (Django)

Posted by HIRANO Satoshi <hi...@aist.go.jp>.
On 2011/11/16, at 14:23, Li Lirong wrote:
> Hi,
> 
> Thank you very much.  This is really helpful!

You are welcome.

The page says the point is this. 

   input_stream = self.request.environ['wsgi.input']
   output_stream = self.response.out

I used the sample in a test web app and it seems working on Google App Engine.

Have fun!

hirano

> On Wed, Nov 16, 2011 at 9:03 AM, HIRANO Satoshi <hi...@aist.go.jp> wrote:
>> 
>> On 2011/11/15, at 14:24, Li Lirong wrote:
>>> We are developing web application with Django (djangoproject.org).  Is
>>> there any tutorials on exposing thrift web services within the Django
>>> framework?
>> 
>> Hi,
>> 
>> Look at the following page. It explains how you run thrift on a WSGI framework. I'm not sure you can do the same thing for Django, but Django is a WSGI framework.
>> 
>>  http://symfoware.blog68.fc2.com/blog-entry-320.html
>> 
>> It's written in Japanese. Look at sample code.
>> 
>> 1) Copy IOStreamTransport.py into thrift/transport.
>> 2) Compile sample.thrift
>> 3) Run main.py on python.
>> 4) Write a client. A sample is provided near the bottom of the page.
>> 
>> I think the IOStreamTransport.py should be included in the distribution package.
>> 
>> @@@@@@@@@@@@@@  Life is beautiful and full of surprises.
>> 
>> HIRANO Satoshi, Ph.D.         <hi...@aist.go.jp>
>> AIST: National Institute of Advanced Industrial Science and Technology  JAPAN
>> 
>> 
>> 

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

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

平野 聡   独立行政法人 産業技術総合研究所 情報技術研究部門
                 TEL 029-861-5166
                 産総研内 222-45166



Re: Integrating thrift with Python web framework (Django)

Posted by Li Lirong <le...@gmail.com>.
Hi,

Thank you very much.  This is really helpful!

Cheers

On Wed, Nov 16, 2011 at 9:03 AM, HIRANO Satoshi <hi...@aist.go.jp> wrote:
>
> On 2011/11/15, at 14:24, Li Lirong wrote:
>> We are developing web application with Django (djangoproject.org).  Is
>> there any tutorials on exposing thrift web services within the Django
>> framework?
>
> Hi,
>
> Look at the following page. It explains how you run thrift on a WSGI framework. I'm not sure you can do the same thing for Django, but Django is a WSGI framework.
>
>  http://symfoware.blog68.fc2.com/blog-entry-320.html
>
> It's written in Japanese. Look at sample code.
>
> 1) Copy IOStreamTransport.py into thrift/transport.
> 2) Compile sample.thrift
> 3) Run main.py on python.
> 4) Write a client. A sample is provided near the bottom of the page.
>
> I think the IOStreamTransport.py should be included in the distribution package.
>
> @@@@@@@@@@@@@@  Life is beautiful and full of surprises.
>
> HIRANO Satoshi, Ph.D.         <hi...@aist.go.jp>
> AIST: National Institute of Advanced Industrial Science and Technology  JAPAN
>
>
>

Module to integrate thrift with Python web framework (WSGI, Django)

Posted by HIRANO Satoshi <hi...@aist.go.jp>.
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.         <hi...@aist.go.jp>
AIST: National Institute of Advanced Industrial Science and Technology


Re: Integrating thrift with Python web framework (Django)

Posted by HIRANO Satoshi <hi...@aist.go.jp>.
On 2011/11/15, at 14:24, Li Lirong wrote:
> We are developing web application with Django (djangoproject.org).  Is
> there any tutorials on exposing thrift web services within the Django
> framework?

Hi,

Look at the following page. It explains how you run thrift on a WSGI framework. I'm not sure you can do the same thing for Django, but Django is a WSGI framework.

 http://symfoware.blog68.fc2.com/blog-entry-320.html

It's written in Japanese. Look at sample code.

1) Copy IOStreamTransport.py into thrift/transport.
2) Compile sample.thrift
3) Run main.py on python.
4) Write a client. A sample is provided near the bottom of the page.

I think the IOStreamTransport.py should be included in the distribution package.

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

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