django and thrift

804 views
Skip to first unread message

leon

unread,
Sep 7, 2011, 2:18:25 AM9/7/11
to Django users
Hi,

I have one question. Is it possible to use Django to write thrift
(http://thrift.apache.org/) formated web service? If it is possible,
is there any sample?

Thank you!

Malcolm Box

unread,
Sep 7, 2011, 8:57:32 AM9/7/11
to django...@googlegroups.com
Possible, maybe. But almost certainly the wrong thing to do.

Thrift is a binary protocol that doesn't look much like HTTP. Attempting to use a web framework to handle it will cause grief.

Use the Thrift Python bindings to write a server, either a straight Thrift server or using Twisted. Then if you need access to things like the Django ORM then use those in the server.

Don't try to tie handling Thrift into the Django URL/HttpRequest/HttpResponse cycle or you'll go mad.

Malcolm

Li Lirong

unread,
Sep 7, 2011, 10:27:39 PM9/7/11
to django...@googlegroups.com
Hi,
Thank you for your comments. I am new to both django and thrift. I
am hosting my service with a shared web hosting server. That's why I
want to use django to expose a thrift API. Basically, what I want is
to have a web service that can be consumed by both python and c++. Do
you think this is possible with django?

Thanks.

> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>

Malcolm Box

unread,
Sep 8, 2011, 4:25:24 AM9/8/11
to django...@googlegroups.com
On 8 September 2011 03:27, Li Lirong <leon.li...@gmail.com> wrote:
Hi,
Thank you for your comments.  I am new to both django and thrift.  I
am hosting my service with a shared web hosting server.  That's why I
want to use django to expose a thrift API.  Basically, what I want is
to have a web service that can be consumed by both python and c++.  Do
you think this is possible with django?


It's entirely possible, but I'd suggest not using Thrift as the transport protocol in this case. You'd be better off having a REST over HTTP service with payloads in JSON.

Li Lirong

unread,
Sep 8, 2011, 5:15:03 AM9/8/11
to django...@googlegroups.com
Thank you! Using json may be a good idea. I will consider this carefully.

Ryan R

unread,
Apr 10, 2014, 3:23:17 PM4/10/14
to django...@googlegroups.com
This is an old thread, but responding in case anyone interested in django+thrift comes across this. Implementing Django/thrift server is not difficult to do and it has advantags over JSON/webservice, namely less overhead and simplified serialization/deserialization of strongly typed complex objects, which you will appreciate if deserializing into strongly typedl languages such as C++.

The easiest way is to create a new Django command that implements the server. (This is how other django modules with daemons such as Django-celery do it.) You can follow the directions in the Django documentation here: 
https://docs.djangoproject.com/en/dev/howto/custom-management-commands/

In your command script, e.g. thrift_server.py, you put in the usual thrift server initialization:

import sys
from django.core.management.base import BaseCommand

from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer

#import thrift files here

#now define the service handler according to your thrift method declaration
class ServiceHandler:
def __init__(self):
pass
#self.log = {}
def thriftMethodName(self, arg):
print "hello world!"
#here you have access to anything in the django framework
return True

class Command(BaseCommand):
def handle(self, *args, **kwargs):
handler = ServiceHandler()
processor = SaleService.Processor(handler)
transport = TSocket.TServerSocket(port=9090)
tfactory = TTransport.TBufferedTransportFactory()
pfactory = TBinaryProtocol.TBinaryProtocolFactory()

server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)

# You could do one of these for a multithreaded server
#server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
#server = TServer.TThreadPoolServer(processor, transport, tfactory, pfactory)

self.stdout.write('Starting thrift server...')
server.serve()
self.stdout.write('done.')

Then you can run the server like so:

(virtualenv) /django_project/ > python manage.py thrift_server

It will run as a daemon, use ctrl+c to exit.
Reply all
Reply to author
Forward
0 new messages