python、suds 和客户端证书:SAXParseException:格式不正确(无效令牌)

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

我已经使用 python (2.6.6) 和 suds 实现了一个 SOAP 客户端。服务器需要来自客户端的证书进行身份验证。为了在 python 中实现这一点并使用泡沫,我使用了 this answer from Andre Miras。这似乎也有效,因为我可以从服务器访问和获取 WSDL。但我还有另一个问题。解析 WSDL 时出现 xml.sax._exceptions.SAXParseException: <unknown>:1:1: not well-formed (invalid token) 错误。

为了检查我是否真的可以获得 WSDL,我已经操作了 "open" 方法(请注意 "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

如果我 运行 脚本我得到输出:

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)

为了清楚起见,我删除了 WSDL。但是我还有一个 Java 客户端,它使用 SOAP 服务并且可以成功读取和解析 WSDL。

有人知道哪里出了问题吗?

我发现我必须替换以下几行。注释掉的代码是旧代码,没有注释的那行是新的新代码:

#import io
import StringIO

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

不知道为什么,但这对我有用(仅更改 io.StringIO(resp.content) 不起作用)。