简体   繁体   中英

python, suds and client certificate: SAXParseException: not well-formed (invalid token)

I have implementing a SOAP client with python (2.6.6) and suds. The server needs a certificate from the client for authentication. For implementing this in python and with suds I have use this answer from Andre Miras . This seem's also to work because I can access and get the WSDL from the server. But I have another problem. While parsing the WSDL I get a xml.sax._exceptions.SAXParseException: <unknown>:1:1: not well-formed (invalid token) error.

To check if I can realy get the WSDL I have manipulate the "open" method (please note the "print"s)

def open(self, request):
    """
    Fetches the WSDL using cert.
    """
    print "11 "
#        self.addcredentials(request)
    resp = requests.get(request.url, data=request.message,
                         headers=request.headers, cert=self.cert)
    result = io.StringIO(resp.content.decode('utf-8'))
    print str(result.getvalue())
    print "<<WSDL END>>"
    return result

If I run the script I get the output:

11
<?xml version='1.0' encoding='UTF-8'?><wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://webService.net.app.my.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="CStatisticService" targetNamespace="http://webService.net.app.my.com/">
  <wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://webService.net.app.my.com/" elementFormDefault="unqualified" targetNamespace="http://webService.net.app.my.com/" version="1.0">

  <xs:element name="getFileActionsNmbr" type="tns:getFileActionsNmbr"/>
...
...
...
  </wsdl:service>
</wsdl:definitions>
<<WSDL END>>
Traceback (most recent call last):
  File "./c-App_jvmThreads", line 63, in <module>
    client = suds.client.Client(wsdl_url, headers=headers, transport=t)
  File "/usr/lib/python2.6/site-packages/suds/client.py", line 112, in __init__
    self.wsdl = reader.open(url)
  File "/usr/lib/python2.6/site-packages/suds/reader.py", line 152, in open
    d = self.fn(url, self.options)
  File "/usr/lib/python2.6/site-packages/suds/wsdl.py", line 136, in __init__
    d = reader.open(url)
  File "/usr/lib/python2.6/site-packages/suds/reader.py", line 79, in open
    d = self.download(url)
  File "/usr/lib/python2.6/site-packages/suds/reader.py", line 101, in download
    return sax.parse(string=content)
  File "/usr/lib/python2.6/site-packages/suds/sax/parser.py", line 136, in parse
    sax.parse(source)
  File "/usr/lib64/python2.6/xml/sax/expatreader.py", line 107, in parse
    xmlreader.IncrementalParser.parse(self, source)
  File "/usr/lib64/python2.6/xml/sax/xmlreader.py", line 123, in parse
    self.feed(buffer)
  File "/usr/lib64/python2.6/xml/sax/expatreader.py", line 211, in feed
    self._err_handler.fatalError(exc)
  File "/usr/lib64/python2.6/xml/sax/handler.py", line 38, in fatalError
    raise exception
  xml.sax._exceptions.SAXParseException: <unknown>:1:1: not well-formed (invalid token)

I have cut the WSDL because clarity. But I have also a Java client which use the SOAP service and can read and parse the WSDL successfully.

Anybody knows what's can be wrong?

I found I have to replace the following lines. The commented out code is the old code, the uncommented line the new new code:

#import io
import StringIO

...
#        result = io.StringIO(resp.content.decode('utf-8'))
    result = StringIO.StringIO(resp.content)

Not sure why, but this works for me (changing only io.StringIO(resp.content) doesn't work).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM