繁体   English   中英

python suds错误解析SOAP响应

[英]python suds error parsing SOAP response

我正在使用脚本从计费系统中获取信息(省略IP):

from suds.client import Client

from netaddr import *
import logging

logging.basicConfig(filename='provisionSV.log',format='%(asctime)s %(levelname)s  (message)s',level=logging.INFO)
logging.getLogger('suds.client').setLevel(logging.DEBUG)
logging.getLogger('suds.transport').setLevel(logging.DEBUG)

url = 'http://X.X.X.X:X/services/X?wsdl'
client = Client(url)
resultGet = client.service.getCustomerAllowancesRequestType_Search('myself', 100000000000000, 309999900000001)

在日志中,我可以看到请求很好,我得到了响应:

HEADERS: {'Soapaction': b'"urn:getCustomerAllowancesRequestType_Search"', 'Content-Type': 'text/xml; charset=utf-8', 'SOAPAction': b'"urn:getCustomerAllowancesRequestType_Search"', 'Content-type': 'text/xml; charset=utf-8'}
MESSAGE:
b'<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-   ENV="http://schemas.xmlsoap.org/soap/envelope/"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns1="http://tresoap.intecbilling.com/2.0/SEP" xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns2="GX"><SOAP-ENV:Header/> <ns0:Body><ns1:getCustomerAllowancesRequestType_SearchRequest>  <ns2:REQUESTOR>myself</ns2:REQUESTOR><ns2:ON_BEHALF_OF>100000000000000</ns2:ON_BEHALF_OF>  <ns2:EXTERNAL_CUSTOMER_ID>309999900000001</ns2:EXTERNAL_CUSTOMER_ID>  </ns1:getCustomerAllowancesRequestType_SearchRequest></ns0:Body></SOAP-ENV:Envelope>'
2014-12-04 11:26:32,985 DEBUG received:
CODE: 200
HEADERS: Date: Thu, 04 Dec 2014 11:40:00 GMT
Content-Type: multipart/related;    boundary=MIMEBoundaryurn_uuid_CFD62554D8E1A6B4381417693216561; type="text/xml"; start=" <0.urn:uuid:CFD62554D8E1A6B4381417693216562@apache.org>"
Connection: close
Server: Jetty(6.1.26)


MESSAGE:
b'--MIMEBoundaryurn_uuid_CFD62554D8E1A6B4381417693216561\r\nContent-Type: text/xml;  charset=utf-8\r\nContent-Transfer-Encoding: 8bit\r\nContent-ID:  <0.urn:uuid:CFD62554D8E1A6B4381417693216562@apache.org>\r\n\r\n<?xml version=\'1.0\'  encoding=\'utf-8\'?><soapenv:Envelope  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body> <getCustomerAllowancesRequestType_SearchResponse  xmlns="http://tresoap.intecbilling.com/2.0/X" xmlns:tns="GX"><tns:RESULT> <tns:MESSAGE_CODE>0</tns:MESSAGE_CODE><tns:MESSAGE_TEXT>No End User Package for the End- User with External_Customer_Id \'309999900000001\'.</tns:MESSAGE_TEXT></tns:RESULT> </getCustomerAllowancesRequestType_SearchResponse></soapenv:Body></soapenv:Envelope>\r\n-- MIMEBoundaryurn_uuid_CFD62554D8E1A6B4381417693216561--'
2014-12-04 11:26:32,986 DEBUG HTTP succeeded:
b'--MIMEBoundaryurn_uuid_CFD62554D8E1A6B4381417693216561\r\nContent-Type: text/xml;  charset=utf-8\r\nContent-Transfer-Encoding: 8bit\r\nContent-ID:  <0.urn:uuid:CFD62554D8E1A6B4381417693216562@apache.org>\r\n\r\n<?xml version=\'1.0\'  encoding=\'utf-8\'?><soapenv:Envelope  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body> <getCustomerAllowancesRequestType_SearchResponse  xmlns="http://tresoap.intecbilling.com/2.0/SEP" xmlns:tns="GX"><tns:RESULT> <tns:MESSAGE_CODE>0</tns:MESSAGE_CODE><tns:MESSAGE_TEXT>No End User Package for the End- User with External_Customer_Id \'309999900000001\'.</tns:MESSAGE_TEXT></tns:RESULT> </getCustomerAllowancesRequestType_SearchResponse></soapenv:Body></soapenv:Envelope>\r\n-- MIMEBoundaryurn_uuid_CFD62554D8E1A6B4381417693216561--'
2014-12-04 11:26:32,986 ERROR <suds.sax.document.Document object at 0x2b6e3d8ae550>

但是我在应用程序中得到了一个sax解析异常:

Traceback (most recent call last):
File "/usr/local/lib/python3.4/xml/sax/expatreader.py", line 207, in feed
   self._parser.Parse(data, isFinal)
xml.parsers.expat.ExpatError: syntax error: line 1, column 0

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/root/fciannel/python-project/autheudsessions/venv/lib/python3.4/site-packages/suds/client.py", line 521, in __call__
return client.invoke(args, kwargs)
File "/root/fciannel/python-project/autheudsessions/venv/lib/python3.4/site-packages/suds/client.py", line 581, in invoke
result = self.send(soapenv)
File "/root/fciannel/python-project/autheudsessions/venv/lib/python3.4/site-packages/suds/client.py", line 619, in send
description=tostr(e), original_soapenv=original_soapenv)
File "/root/fciannel/python-project/autheudsessions/venv/lib/python3.4/site-packages/suds/client.py", line 661, in process_reply
replyroot = _parse(reply)
File "/root/fciannel/python-project/autheudsessions/venv/lib/python3.4/site-packages/suds/client.py", line 832, in _parse
return Parser().parse(string=string)
File "/root/fciannel/python-project/autheudsessions/venv/lib/python3.4/site-packages/suds/sax/parser.py", line 133, in parse
sax.parse(source)
File "/usr/local/lib/python3.4/xml/sax/expatreader.py", line 107, in parse
xmlreader.IncrementalParser.parse(self, source)
File "/usr/local/lib/python3.4/xml/sax/xmlreader.py", line 123, in parse
self.feed(buffer)
File "/usr/local/lib/python3.4/xml/sax/expatreader.py", line 211, in feed
self._err_handler.fatalError(exc)
File "/usr/local/lib/python3.4/xml/sax/handler.py", line 38, in fatalError
raise exception xml.sax._exceptions.SAXParseException: 
<unknown>:1:0: syntax error

我得到的答案有什么问题?

嗯,HTTP响应主体不是格式正确的XML,因为它包装在MIME / multipart结构中。 据我所知,suds不支持“多部分/相关”内容类型的响应。 我认为您必须将回复传递给XML解析器之前自己反汇编。

我认为解决问题的最佳方法可能是编写自定义MessagePlugin并实现receive received(context)方法(实际的回复是上下文的一部分)。

非常感谢,这项工作奏效了,我写了一个删除MIME部分的插件。 这是一个简单的示例:

class MyPlugin(MessagePlugin):
    def received(self, context):
        answer = context.reply
        answerDecoded = answer.decode()
        xmlMessage = re.search(r'(<soapenv\:Envelope.*)\r', answerDecoded)
        replyFinal = xmlMessage.group(1)+'\n'
        replyFinalDecoded = replyFinal.encode()
        context.reply = replyFinalDecoded

到目前为止,一切似乎还不错。

非常感谢您的提示!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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