Skip to content

Instantly share code, notes, and snippets.

@plq
Created September 26, 2011 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save plq/1242760 to your computer and use it in GitHub Desktop.
Save plq/1242760 to your computer and use it in GitHub Desktop.
django wrapper for rpclib
# This is not yet included in main rpclib distribution because
# it does a lot of unnecessary copying. However, it's reportedly
# working fine, so if you're not caring that much about performance
# you can use this.
from rpclib.server.wsgi import WsgiApplication
from cStringIO import StringIO
from django.http import HttpResponse
class DjangoApplication(WsgiApplication):
def __call__(self, request):
django_response = HttpResponse()
def start_response(status, headers):
status, reason = status.split(' ', 1)
django_response.status_code = int(status)
for header, value in headers:
django_response[header] = value
environ = request.META.copy()
body = ''.join(['%s=%s' % v for v in request.POST.items()])
environ['CONTENT_LENGTH'] = len(body)
environ['wsgi.input'] = StringIO(body)
environ['wsgi.multithread'] = False
response = WsgiApplication.__call__(self, environ, start_response)
django_response.content = "\n".join(response)
return django_response
from rpclib.server.django import DjangoApplication # not real import path.
from rpclib.model.primitive import String, Integer
from rpclib.model.complex import Iterable
from rpclib.service import ServiceBase
from rpclib.interface.wsdl import Wsdl11
from rpclib.protocol.soap import Soap11
from rpclib.application import Application
from rpclib.decorator import rpc
class HelloWorldService(ServiceBase):
@rpc(String, Integer, _returns=Iterable(String))
def say_hello(ctx, name, times):
for i in xrange(times):
yield 'Hello, %s' % name
hello_world_service = DjangoApplication(Application([HelloWorldService],
'some.tns',
interface=Wsdl11(),
in_protocol=Soap11(),
out_protocol=Soap11()
))
@plq
Copy link
Author

plq commented Mar 7, 2012

also, this is part of the rpclib already. there's an example project here:

https://github.com/arskom/rpclib/tree/a3b14df62da696960eb3157b65f5032f295cddf3/examples/django

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