de error de Django soaplib
-
21-08-2019 - |
Pregunta
Estoy tratando de hacer un pequeño servicio web "Hello World" con Django siguiendo algunos tutoriales, pero estoy golpeando la misma barrera una y otra vez. He definido un view.py y 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
Y parece que la "respuesta = Super ...." línea me está dando problemas. Cuando cargo hasta /hello_world/services.wsdl mapeados en url.py me sale:
AttributeError en /hello_world/service.wsdl 'Módulo' objeto no tiene atributo 'toString'
En el mensaje de error completo, ver aquí: http://saers.dk:8000/hello_world/service.wsdl
¿Tiene alguna sugerencia en cuanto a por qué me sale este error? Y donde se define elementtree?
Solución
@zdmytriv la línea
soap_app_response = super(BaseSOAPWebService, self).__call__(environ, start_response)
debe ser similar
soap_app_response = super(DjangoSoapApp, self).__call__(environ, start_response)
A continuación, el ejemplo funciona.
Otros consejos
No estoy seguro si esto va a resolver su problema, pero el decorador en su función hola dice que se supone que debe devolver una matriz de cadenas, pero en realidad se está devolviendo una cadena
Trate _returns = soap_types.String lugar
Ray
Copiar / pegar desde mi servicio:
# 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
fragmento tiene un error. Leer últimos dos comentarios desde esa URL.