【问题标题】:Django - WebService with soaplib xml characters or ampersand scaping?Django - 带有soap lib xml字符或&符号抓取的Web服务?
【发布时间】:2012-12-18 16:44:28
【问题描述】:

这是我的第一个问题,所以我会尽力做到最好。

我正在尝试使用 Soaplib 2.0 在 Python 2.6 和 Django 1.4 中实现 WebService 服务器。

Web 服务正在运行,并且 Django 在 Django 开发服务器中正常提供服务。

这里是 Django 视图和 URL 的代码:

views.py

from soaplib.core.service import rpc, DefinitionBase, soap
from soaplib.core.model.primitive import String, Integer, Boolean
from soaplib.core.model.clazz import Array
from soaplib.core import Application
from soaplib.core.server.wsgi import Application as WSGIApplication
from django.http import HttpResponse

class HelloWorldService(DefinitionBase):
    @soap(String,Integer,_returns=Array(String))
    def say_smello(self,name,times):
        results = []
        for i in range(0,times):
            results.append('Hello, %s'%name)
        return results
    @soap(String,_returns=Boolean)
    def xml(self,xml):
        result = xml
        return True
    @soap(String,_returns=String)
    def xml2(self,xml2):
        return xml2



class DjangoSoapApp(WSGIApplication):
    csrf_exempt = True

    def __init__(self, services, tns):
        """Create Django view for given SOAP soaplib services and
tns"""

        return super(DjangoSoapApp,
            self).__init__(Application(services, tns))

    def __call__(self, request):
        django_response = HttpResponse()

        def start_response(status, headers):
            django_response.status_code = int(status.split(' ', 1)[0])
            for header, value in headers:
                django_response[header] = value

        response = super(DjangoSoapApp, self).__call__(request.META,
            start_response)
        django_response.content = '\n'.join(response)

        return django_response

my_soap_service = DjangoSoapApp([HelloWorldService], __name__)

urls.py

url(r'^soap/wsdl$', 'soap.views.my_soap_service'),
url(r'^soap/$', 'soap.views.my_soap_service'),

问题是,我想将 XML 传递给 WebService 方法 xml 或 xml2,然后使用 XML 中的信息进行处理。我收到错误。

如果我传递一个没有像“&”这样的字符的简单字符串,一切正常,例如:

首先让我们导入 suds 并将 suds 设置为 Debug:

from suds.client import Client
import logging
logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.client').setLevel(logging.DEBUG)

现在让我们开始调用 de WS:

WSDL = "http://server.test/soap/wsdl"
client = Client(WSDL)
client.service.xml('x and y')

完美运行,我得到“真”,SUDS 的日志显示我正在这样做:

DEBUG:suds.client:sending to (http://server.test/soap/wsdl)
message:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:ns0="soap.views" xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header/>
   <ns1:Body>
      <ns0:xml>
         <ns0:xml>x and y</ns0:xml>
      </ns0:xml>
   </ns1:Body>
</SOAP-ENV:Envelope>
DEBUG:suds.client:headers = {'SOAPAction': '"xml"', 'Content-Type': 'text/xml; charset=utf-8'}
DEBUG:suds.client:http succeeded:
<?xml version='1.0' encoding='utf-8'?>
<senv:Envelope xmlns:wsa="http://schemas.xmlsoap.org/ws/2003/03/addressing" xmlns:plink="http://schemas.xmlsoap.org/ws/2003/05/partner-link/" xmlns:xop="http://www.w3.org/2004/08/xop/include" xmlns:senc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:s0="soap.views" xmlns:s12env="http://www.w3.org/2003/05/soap-envelope/" xmlns:s12enc="http://www.w3.org/2003/05/soap-encoding/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:senv="http://schemas.xmlsoap.org/soap/envelope/"  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"><senv:Body><s0:xmlResponse><s0:xml    Result>true</s0:xmlResult></s0:xmlResponse></senv:Body></senv:Envelope>
True

但如果我这样做:

client.service.xml('x &  y')

不工作,它以 suds 超时结束,服务器报告管道损坏,这是 suds 日志告诉我正在推送的内容:

DEBUG:suds.client:sending to (http://server.test/soap/wsdl)
message:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:ns0="soap.views" xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header/>
   <ns1:Body>
      <ns0:xml>
         <ns0:xml>x &amp;  y</ns0:xml>
      </ns0:xml>
   </ns1:Body>
</SOAP-ENV:Envelope>
DEBUG:suds.client:headers = {'SOAPAction': '"xml"', 'Content-Type': 'text/xml; charset=utf-8'}

这是关于 suds 的错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/site-packages/suds/client.py", line 542, in __call__
    return client.invoke(args, kwargs)
  File "/usr/lib/python2.6/site-packages/suds/client.py", line 602, in invoke
    result = self.send(soapenv)
  File "/usr/lib/python2.6/site-packages/suds/client.py", line 643, in send
    reply = transport.send(request)
  File "/usr/lib/python2.6/site-packages/suds/transport/https.py", line 64, in send
    return  HttpTransport.send(self, request)
  File "/usr/lib/python2.6/site-packages/suds/transport/http.py", line 77, in send
    fp = self.u2open(u2request)
  File "/usr/lib/python2.6/site-packages/suds/transport/http.py", line 118, in u2open
    return url.open(u2request, timeout=tm)
  File "/usr/lib/python2.6/urllib2.py", line 391, in open
    response = self._open(req, data)
  File "/usr/lib/python2.6/urllib2.py", line 409, in _open
    '_open', req)
  File "/usr/lib/python2.6/urllib2.py", line 369, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.6/urllib2.py", line 1190, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/usr/lib/python2.6/urllib2.py", line 1165, in do_open
    raise URLError(err)
urllib2.URLError: <urlopen error timed out>

感谢这个链接:http://grokbase.com/p/python/soap/125nhj9bdm/python-suds-xml-encoding-issue 我发现是的,如果我发布类似的 WebService:

client.service.xml("x &amp;amp; y")

我猜对了:

DEBUG:suds.client:sending to (http://server.test/soap/wsdl)
message:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:ns0="soap.views" xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header/>
   <ns1:Body>
      <ns0:xml>
         <ns0:xml>x &amp;amp; y</ns0:xml>
      </ns0:xml>
   </ns1:Body>
</SOAP-ENV:Envelope>
DEBUG:suds.client:headers = {'SOAPAction': '"xml"', 'Content-Type': 'text/xml; charset=utf-8'}
True

如果我使用 xml2 方法,那么我可以看到返回 WebService 的内容是这样的:

client.service.xml2("x &amp;amp; y")

以及 Suds 的日志:

DEBUG:suds.client:sending to (http://server.test/soap/wsdl)
message:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:ns0="soap.views" xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header/>
   <ns1:Body>
      <ns0:xml2>
         <ns0:xml2>x &amp;amp; y</ns0:xml2>
      </ns0:xml2>
   </ns1:Body>
</SOAP-ENV:Envelope>
DEBUG:suds.client:headers = {'SOAPAction': '"xml2"', 'Content-Type': 'text/xml; charset=utf-8'}


DEBUG:suds.client:http succeeded:
<?xml version='1.0' encoding='utf-8'?>
<senv:Envelope xmlns:wsa="http://schemas.xmlsoap.org/ws/2003/03/addressing" xmlns:plink="http://schemas.xmlsoap.org/ws/2003/05/partner-link/" xmlns:xop="http://www.w3.org/2004/08/xop/include" xmlns:senc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:s0="soap.views" xmlns:s12env="http://www.w3.org/2003/05/soap-envelope/" xmlns:s12enc="http://www.w3.org/2003/05/soap-encoding/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:senv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"><senv:Body><s0:xml2Response><s0:xml2Result>x &amp; y</s0:xml2Result></s0:xml2Response></senv:Body></senv:Envelope>
x & y

但我认为问题不在SUDS,因为我使用这个版本的suds将XML推送到java WebServices,但是我没有找到这个soaplib的方法。

有什么想法吗?这让我有点疯狂xD

我的最终目标是使用 Soaplib 提供 Web 服务,并从 Java 中的肥皂客户端向其推送 XML。

谢谢

【问题讨论】:

  • 是的,看起来像是您的客户端肥皂库中的一个错误。
  • 你的意思是泡沫?但是对字符进行缩放是正确的,不是吗?
  • 我为您添加了一个可能的答案。顺便说一句,这是“逃避”而不是“逃避”
  • 谢谢,但不是很好......和“逃跑”的tnanks......英语不是我的第一语言:s

标签: python django suds soaplib


【解决方案1】:

好的,解决了:D

我只能更改服务器端,所以我将代码迁移到 spyne 并像 Charm 一样工作。

https://github.com/arskom/spyne

我在他们的网站上找到了这个:

 Soaplib-2.0 was never released as a stable package, but the branch is still available

他们从 soaplib 到 Rpclib,然后从那里到 Spyne。

我确实发现了一个新错误,这次是在 suds 上,但我只是用于测试的 suds,但如果我使用其他客户端,SoapUI 或 Java 客户端,效果很好。

使用 PIP 安装后,这是 Django 代码:

urls.py

url(r'^testws/\?wsdl$', 'testmo.views.ws_test'),
url(r'^testws/$', 'testmo.views.ws_test'),

views.py

from django.views.decorators.csrf import csrf_exempt
from spyne.server.django import DjangoApplication
from spyne.model.primitive import String
from spyne.service import ServiceBase
from spyne.interface.wsdl import Wsdl11
from spyne.protocol.soap import Soap11
from spyne.application import Application
from spyne.decorator import srpc



class ServiceWsTest(ServiceBase):
    @srpc(String, _returns=String)
    def testMethod(string):
        return string

ws_test = csrf_exempt(DjangoApplication(Application([ServiceWsTest],
    'http://example.com',
    in_protocol=Soap11(),
    out_protocol=Soap11(),
    interface=Wsdl11(),
)))

从现在开始,我将继续使用这个库

【讨论】:

    【解决方案2】:

    也许您可以将它不转义地包装在 cdata 标记中。更多信息http://en.wikipedia.org/wiki/CDATA

    <![CDATA[
    x & y
    ]]>
    

    【讨论】:

    • 实际上这行得通....我不知道这一点(总是很高兴知道一些新的东西),但这意味着肥皂客户端必须使用这个标签 CDATA 推送 XML,并且这是不可能的,因为发布到 webService 的客户端数量众多......并且一个试图推送 XML 的应用程序是一个私有软件,我无法控制它:s 真的谢谢你
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-20
    相关资源
    最近更新 更多