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

时间:2016-07-19 20:05:46

标签: python soap wsdl suds

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

要检查我是否可以真正获得WSDL我操纵了“打开”方法(请注意“打印”)

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,因为清晰度。但我还有一个使用SOAP服务的Java客户端,可以成功读取和解析WSDL。

有人知道什么是错的吗?

1 个答案:

答案 0 :(得分:0)

我发现我必须更换以下行。注释掉的代码是旧代码,未注释的代码是新的代码:

#import io
import StringIO

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

不确定原因,但这适用于我(仅更改io.StringIO(resp.content)无效)。