我正在尝试使用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时,我得到:
/hello_world/service.wsdl上的AttributeError 'module'对象没有属性'tostring'
有关完整的错误消息,请参阅此处: http://saers.dk:8000/hello_world/service.wsdl
您对我收到此错误的原因有什么建议吗? ElementTree在哪里定义?
答案 0 :(得分:1)
@zdmytriv该行
soap_app_response = super(BaseSOAPWebService, self).__call__(environ, start_response)
应该看起来像
soap_app_response = super(DjangoSoapApp, self).__call__(environ, start_response)
那么你的例子就可以了。
答案 1 :(得分:0)
不确定这是否会解决您的问题,但函数问候中的装饰器说它假设返回一个String Array,但实际上是在返回一个String
尝试_returns = soap_types.String而不是
雷
答案 2 :(得分: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 snippet有一个错误。阅读该网址的最后两条评论。