python、suds 和客户端证书:SAXParseException:格式不正确(令牌无效) [英] python, suds and client certificate: SAXParseException: not well-formed (invalid token)
问题描述
我用 python (2.6.6) 和 suds 实现了一个 SOAP 客户端.服务器需要来自客户端的证书进行身份验证.为了在 python 和 suds 中实现这一点,我使用了 这个来自 Andre Miras 的答案.这似乎也有效,因为我可以从服务器访问和获取 WSDL.但我还有另一个问题.在解析 WSDL 时,我收到 xml.sax._exceptions.SAXParseException: <unknown>:1:1: 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.
为了检查我是否真的能得到 WSDL,我操纵了open"方法(请注意print")
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)
因为清晰,我已经削减了 WSDL.但我还有一个 Java 客户端,它使用 SOAP 服务并且可以成功读取和解析 WSDL.
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)
不知道为什么,但这对我有用(仅更改 io.StringIO(resp.content)
不起作用).
Not sure why, but this works for me (changing only io.StringIO(resp.content)
doesn't work).
这篇关于python、suds 和客户端证书:SAXParseException:格式不正确(令牌无效)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!