我试图与Django的一个小的“Hello World” Web服务以下几个教程,但我一遍又一遍地打同样的障碍。我已经定义了一个view.py和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

和它似乎是“响应=超级....”行给我找麻烦。当我加载在url.py映射/hello_world/services.wsdl我得到:

AttributeError的在/hello_world/service.wsdl “模块”对象没有属性“的toString”

有关完整的错误消息,在这里看到: http://saers.dk:8000/hello_world/service.wsdl

你有什么建议,为什么我得到这个错误?和其中ElementTree的定义

有帮助吗?

解决方案

@zdmytriv线

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

应该像

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

那么你的示例的工作原理。

其他提示

不知道这是否会解决您的问题,但你的函数你好装饰说,这是假设返回一个字符串数组,但你实际上是返回一个字符串

尝试_returns = soap_types.String代替

从我的服务

复制/粘贴:

# 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 片断有一个错误。从URL中读取最后两个注释。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top