serving SOAP service with soaplib, consuming with suds

253 views
Skip to first unread message

Ricko

unread,
Oct 19, 2010, 5:13:19 PM10/19/10
to Django users
Hi all,

I am trying to make a SOAP service in Django via soaplib(1.0.0 - beta
8) and consume the same service for testing purposes with SUDS (0.4).

I can consume external SOAP services with SUDS no problem, but am
running into problems when trying to consume my own service.

My service is as follows:

#soap_service.py file

import StringIO

from soaplib.service import rpc, DefinitionBase
from soaplib.serializers import primitive as soap_types

class DumbStringIO(StringIO.StringIO):
def read(self, n):
return self.getvalue()


class DjangoSoapService(DefinitionBase):

__tns__ = 'http://127.0.0.1:8000'

@rpc(soap_types.String, _returns=soap_types.String)
def hello_world(self, hello_string):
"""
Accepts primitive string and returns the same primitive.
"""
return hello_string

Here is my website.views.py

class MySOAPService(Application):
"""
Creates a WSGI application interface to the SOAP service
"""
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 = request.raw_post_data
environ['CONTENT_LENGTH'] = len(body)
environ['wsgi.input'] = DumbStringIO(body)
environ['wsgi.multithread'] = False
response = super(MySOAPService, self).__call__(environ,
start_response)
django_response.content = "\n".join(response)
return django_response


my_soap_service = MySOAPService([DjangoSoapService],
DjangoSoapService.__tns__)

and here is my urls.py file:

urlpatterns += patterns('website.views',
(r'^hello_world/', 'my_soap_service'),
(r'^hello_world/service.wsdl',
'my_soap_service'),
)

I can view the wsdl file no problem, and it correctly shows my method
'hello_world'. However calling the method with suds from the python
shell thus:

>>>from suds.client import Client
client = ('http://127.0.0.1:8000/hello_world/service.wsdl',
cache=None)
response = client.service.hello_world('hi')

gives a stack:

Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Library/Python/2.5/site-packages/suds-0.4-py2.5.egg/suds/
client.py", line 542, in __call__
return client.invoke(args, kwargs)
File "/Library/Python/2.5/site-packages/suds-0.4-py2.5.egg/suds/
client.py", line 602, in invoke
result = self.send(soapenv)
File "/Library/Python/2.5/site-packages/suds-0.4-py2.5.egg/suds/
client.py", line 649, in send
result = self.failed(binding, e)
File "/Library/Python/2.5/site-packages/suds-0.4-py2.5.egg/suds/
client.py", line 708, in failed
raise Exception((status, reason))
Exception: (403, u'FORBIDDEN')


No idea why it's not allowing me to connect, it's all local. Help!!

Scott Hebert (slaptijack)

unread,
Oct 23, 2010, 10:24:15 PM10/23/10
to Django users
Ricko -

You may have already gotten your answer. I believe your problem is
related to Django's CSRF protection. If that's the case, you'll want
to import csrf_exempt from django.views.decorators.csrf into your
views and then wrap the call to MySOAPService in it.

For example:

# views.py
from django.views.decorators.csrf import csrf_exempt

<snip>

my_soap_service = csrf_exempt(MySOAPService([DjangoSoapService],
DjangoSoapService.__tns__))

In my testing that seemed to work:

$ python2.5
Python 2.5.5 (r255:77872, Sep 7 2010, 10:18:54)
[GCC 4.0.1 (Apple Inc. build 5490)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from suds.client import Client

>>> client = Client("http://127.0.0.1:8000/hello_world/service.wsdl", cache=None, faults=False)
>>> response = client.service.hello_world(hello_string="hi")
>>> print response
(200, hi)

Let us know if that works for you.

--
Scott Hebert
http://slaptijack.com

Meenakshi Ithape

unread,
Apr 26, 2012, 9:00:00 AM4/26/12
to django...@googlegroups.com
I done this & successfully run the code.
I want to ask that, can it possible to create client through view instead of command prompt?
Reply all
Reply to author
Forward
0 new messages