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.

On Thursday, September 8, 2011 4:15:03 AM UTC-5, leon wrote:
>
> Thank you!   Using json may be a good idea.  I will consider this 
> carefully.
>
>
> On Thu, Sep 8, 2011 at 4:25 PM, Malcolm Box <[email protected]<javascript:>> 
> wrote:
> >
> >
> > On 8 September 2011 03:27, Li Lirong <[email protected] <javascript:>> 
> 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.
> > M
> >
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Django users" group.
> > To post to this group, send email to [email protected]<javascript:>
> .
> > To unsubscribe from this group, send email to
> > [email protected] <javascript:>.
> > For more options, visit this group at
> > http://groups.google.com/group/django-users?hl=en.
> >
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/239de03e-ceff-4a9e-88c8-4a10b299ccf1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to