Login

SOAP web service with soaplib 0.9+

Author:
wRAR
Posted:
September 28, 2010
Language:
Python
Version:
1.2
Score:
0 (after 0 ratings)

The popular soaplib snippet works only with older soaplib (0.8, for example). This snippet is modifed to work with newer soaplib: it was tested with 0.9.2-alpha3 and 1.0.0-beta4. I've tested suds python client and .NET client.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from soaplib.serializers.primitive import Boolean, String
from soaplib.service import DefinitionBase, rpc
from soaplib.wsgi import Application

from django.http import HttpResponse

# the class with actual web methods
class MySOAPService(DefinitionBase):
    @rpc(String, String, _returns=Boolean)
    def Test(self, f1, f2):
        return True

# the class which acts as a wrapper between soaplib WSGI functionality and Django
class DjangoSoapApp(Application):
    def __call__(self, request):
        # wrap the soaplib response into a Django response object
        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
        response = super(DjangoSoapApp, self).__call__(request.META, start_response)
        django_response.content = '\n'.join(response)
        return django_response

# the view to use in urls.py
my_soap_service = DjangoSoapApp([MySOAPService], __name__)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

andrewk (on March 2, 2011):

nice snippet, but I catch 403 "CSRF token missing or incorrect." any ideas to solve it?

#

ungenio41 (on October 12, 2011):

Try this to get around csrf protection:

from django.http import HttpResponse
from soaplib.wsgi import Application


class DjangoSoapApp(Application):
    csrf_exempt = True

    def __call__(self, request):
        # wrap the soaplib response into a Django response object
        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
        response = super(DjangoSoapApp, self).__call__(
            request.META, start_response)
        django_response.content = '\n'.join(response)
        django_response.csrf_exempt = True
        return django_response

#

Please login first before commenting.