当前位置: 动力学知识库 > 问答 > 编程问答 >

web services - Django soaplib error

问题描述:

I'm trying to make a little "Hello World" webservice with Django following a few tutorials, but I'm hitting the same barrier over and over. I've defined a view.py and soaplib_handler.py:

view.py:

from soaplib_handler import DjangoSoapApp, soapmethod, soap_types

class HelloWorldService(DjangoSoapApp):

__tns__ = 'http://saers.dk/soap/'

@soapmethod(_returns=soap_types.Array(soap_types.String))

def hello(self):

return "Hello World"

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

And it seems the "response = super...." line is giving me trouble. When I load up /hello_world/services.wsdl mapped in url.py I get:

AttributeError at /hello_world/service.wsdl

'module' object has no attribute 'tostring'

For the full error message, see here:

http://saers.dk:8000/hello_world/service.wsdl

Do you have any suggestion as to why I get this error? And where is ElementTree defined?

网友答案:

@zdmytriv The line

soap_app_response = super(BaseSOAPWebService, self).__call__(environ, start_response)

should look like

soap_app_response = super(DjangoSoapApp, self).__call__(environ, start_response)

then your example works.

网友答案:

not sure if this will solve your problem, but the decorator on your function hello says that it is suppose to return a String Array, but you are actually returning a String

Try _returns=soap_types.String instead

Ray

网友答案:

Copy/paste from my service:

# SoapLib Django workaround: http://www.djangosnippets.org/snippets/979/
class DumbStringIO(StringIO):
    """ Helper class for BaseWebService """
    def read(self, n): 
        return self.getvalue()

class DjangoSoapApp(SimpleWSGISoapApp):
    def __call__(self, request):
        """ Makes Django request suitable for SOAPlib SimpleWSGISoapApp class """

        http_response = HttpResponse()

        def start_response(status, headers):
            status, reason = status.split(' ', 1)
            http_response.status_code = int(status)

            for header, value in headers:
                http_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'] = DumbStringIO(body)
        environ['wsgi.multithread'] = False

        soap_app_response = super(BaseSOAPWebService, self).__call__(environ, start_response)

        http_response.content = "\n".join(soap_app_response)

        return http_response

Django snippet has a bug. Read last two comments from that url.

分享给朋友:
您可能感兴趣的文章:
随机阅读: