簡體   English   中英

Django - 帶有soaplib xml字符或&符號的WebService?

[英]Django - WebService with soaplib xml characters or ampersand scaping?

那么這是我的fisrt問題,所以我會盡力做到最好。

我正在嘗試使用Soaplib 2.0在Python 2.6和Django 1.4中實現WebService服務器。

Web服務正在運行,Django在Django開發服務器中正常運行。

這是Django View和Urls的代碼:

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'),

問題是,我想傳遞給WebService方法xml或xml2一個XML,並使用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

我的最終目標是讓Webservice與Soaplib一起使用Java從SOAP客戶端推送XML。

謝謝

好的,解決了: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上,但是我剛用於測試的泡沫,但如果我使用其他客戶端,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(),
)))

從現在開始,我將繼續使用這個庫

也許你可以用cdata標簽包裝它,未轉義。 有關更多信息,請訪問http://en.wikipedia.org/wiki/CDATA

<![CDATA[
x & y
]]>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM