0

在一些教程之后,我正在尝试使用 Django 制作一个小的“Hello World”网络服务,但我一遍又一遍地遇到同样的障碍。我已经定义了 view.py 和 soaplib_handler.py:

视图.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"

肥皂库处理程序.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 时,我得到:

/hello_world/service.wsdl 'module' 对象的 AttributeError 没有属性 'tostring'

有关完整的错误消息,请参见此处: http ://saers.dk:8000/hello_world/service.wsdl

您对我为什么会收到此错误有任何建议吗?ElementTree 在哪里定义?

4

3 回答 3

1

@zdmytriv 这条线

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

应该看起来像

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

然后你的例子有效。

于 2009-12-22T21:44:45.260 回答
0

不确定这是否会解决您的问题,但是您的函数 hello 上的装饰器说它应该返回一个字符串数组,但实际上您返回的是一个字符串

尝试 _returns=soap_types.String 代替

射线

于 2009-04-22T15:06:17.730 回答
0

从我的服务中复制/粘贴:

# 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片段有一个错误。阅读该网址的最后两条评论。

于 2009-07-02T00:07:48.540 回答