Optio's soaplib makes it really straightforward to write SOAP web service views by using a decorator to specify types. Plus it's the only Python library, as of today, which is able to generate WSDL documents for your web service.
You can test it with a soaplib client:
>>> from soaplib.client import make_service_client >>> from foo.views import HelloWorldService >>> client = make_service_client('http://localhost:8000/hello_world/', HelloWorldService()) >>> client.say_hello('John', 2) ['Hello, John', 'Hello, John']
And get an WSDL document:
>>> client.server.wsdl('') '<?xml version=\'1.0\' encoding=\'utf-8\' ?><definitions name="HelloWorldService" ... </definitions>'
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
# soaplib_handler.py from soaplib.wsgi_soap import SimpleWSGISoapApp from soaplib.service import soapmethod from soaplib.serializers import primitive as soap_types from django.http import HttpResponse class DjangoSoapApp(SimpleWSGISoapApp): 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 response = super(SimpleWSGISoapApp, self).__call__(request.META, start_response) django_response.content = "\n".join(response) return django_response # views.py - sample view from soaplib_handler import DjangoSoapApp, soapmethod, soap_types class HelloWorldService(DjangoSoapApp): __tns__ = 'http://my.namespace.org/soap/' @soapmethod(soap_types.String, soap_types.Integer, _returns=soap_types.Array(soap_types.String)) def say_hello(self, name, times): results =  for i in range(0, times): results.append('Hello, %s'%name) return results hello_world_service = HelloWorldService() # urls.py urlpatterns = patterns( '', (r'^hello_world/', 'foo.views.hello_world_service'), (r'^hello_world/service.wsdl', 'foo.views.hello_world_service'), )
More like this
- Stuff by NixonDash 3 weeks, 6 days ago
- Add custom fields to the built-in Group model by jmoppel 3 months ago
- Month / Year SelectDateWidget based on django SelectDateWidget by pierreben 6 months, 2 weeks ago
- Python Django CRUD Example Tutorial by tuts_station 6 months, 4 weeks ago
- Browser-native date input field by kytta 8 months, 1 week ago
This is a great example. Thanks!
This doesn't work for me because of problems with wsgi.input - have you actually successfully run this? The server just hangs for me trying to read the request from the client. I really wish it would work cause it's such a simple way of structuring this and soaplib looked pretty nice.
I've just started to use this (it works). I have 2 comments:
When you use super() you usually pass the class you are in (not the base class).
To automatically follow Django model relationships I have written a little
SoapDjangoArraytype; which will follow a reverse foreign key or m2m relationship:
`` class SoapDjangoArray(soap_types.Array):
@csar: I'm having the same problem. Do you have a solution yet?
Got it: change soaplib_handler into:
from soaplib.wsgi_soap import SimpleWSGISoapApp from soaplib.service import soapmethod from soaplib.serializers import primitive as soap_types import StringIO
from django.http import HttpResponse
class DumbStringIO(StringIO.StringIO): def read(self, n): return self.getvalue()
It also hangs for me also,
emilianoheyns's patch works fine.
The snippet works very well, the only problem I have is that the reply has no namespace defined :S
Just FYI the WSDL issue in soaplib v0.8.1 is fixed in the trunk so emilianoheyns version of soaplib_handler works like a charm.
emilianoheyns patch saved me... one hint: instead of
you can simply use
ealekseev I also got 403 forbidden.
I fixed it by turning off the CSRF middleware in the Django settings file.
To avoid CSRF Error just add:
@csrf_exempt class HelloWorldService(DjangoSoapApp):
soapui is throwing
<s0:Fault>say_helloFault</s0:faultcode> <s0:faultstring>'say_hello'</s0:faultstring> <s0:detail></s0:detail> </s0:Fault>
whenever I try to invoke say_hello service, however it works well with suds client.
@csrf_exempt really work at soaplib 0.8.1 it work too
I have tried to get this snippet to work to no avail. Coded as is. I get a 405 from the web server. What I have left out?
In the disection of the 405, the message indiates that the GET is not allowed, but POST is.
Please login first before commenting.