在Python中解析SOAP响应

2024-05-16 19:41:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图解析来自服务器的SOAP响应。我100%不熟悉SOAP,而且对使用HTTP/HTTPS进行交流非常陌生。我在Ubuntu12.04上使用Python2.7。在

看起来SOAP很像XML。然而,我似乎不能这样来解析它。我尝试过使用ElementTree,但总是出现错误。通过搜索,我得出结论,SOAP标记可能有问题。(我可能离这里很远…如果我在,请告诉我。)

因此,这里是我所拥有的SOAP消息的一个示例,以及我试图如何解析它(这是来自linkpointgateway的实际服务器响应,以防相关)。在

import xml.etree.ElementTree as ET
soap_string = '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Body><fdggwsapi:FDGGWSApiOrderResponse xmlns:fdggwsapi="http://secure.linkpt.net/fdggwsapi/schemas_us/fdggwsapi"><fdggwsapi:CommercialServiceProvider/><fdggwsapi:TransactionTime>Wed Jul 25 10:26:40 2012</fdggwsapi:TransactionTime><fdggwsapi:TransactionID/><fdggwsapi:ProcessorReferenceNumber/><fdggwsapi:ProcessorResponseMessage/><fdggwsapi:ErrorMessage>SGS-002303: Invalid credit card number.</fdggwsapi:ErrorMessage><fdggwsapi:OrderId>1</fdggwsapi:OrderId><fdggwsapi:ApprovalCode/><fdggwsapi:AVSResponse/><fdggwsapi:TDate/><fdggwsapi:TransactionResult>FAILED</fdggwsapi:TransactionResult><fdggwsapi:ProcessorResponseCode/><fdggwsapi:ProcessorApprovalCode/><fdggwsapi:CalculatedTax/><fdggwsapi:CalculatedShipping/><fdggwsapi:TransactionScore/><fdggwsapi:FraudAction/><fdggwsapi:AuthenticationResponseCode/></fdggwsapi:FDGGWSApiOrderResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>'
targetTree = ET.fromstring(soap_string)

这将产生以下错误:

^{pr2}$

从另一个stackoverflowpost我得出结论,SOAP-ENV:Body可能导致了名称空间问题。(我可能错了。)

我做了其他的搜索来找到解析SOAP的好的解决方案,但是大多数都是3年前的。似乎suds是非常推荐的。我想在我走得太远之前得到“更新”的推荐。在

有人能推荐一种可靠(简单)的方法来解析我上面收到的一个SOAP响应吗?如果您能提供一个简单的例子让我开始学习,我将不胜感激(正如我前面所说,我对SOAP完全陌生)。在


Tags: 服务器envhttpstring错误bodyschemassoap
2条回答

我无法找到使用Python的直接方法。我决定改用PHP。在

很像以下内容:

Python:

import subprocess
command = 'php /path/to/script.php "{1}"'.format(soap_string)
process = subprocess.Popen(command, shell = True, stderr = subprocess.PIPE, stdout = subprocess.PIPE)
process.wait()
output = process.communicate()[0]
(error, result, order_id) = output.split(',')

菲律宾比索:

^{pr2}$

此代码仅解析此特定SOAP响应的特定方面。它应该足够让你去解决你的问题。在

当谈到PHP时,我是一个完全的新手(我使用了一些Perl,所以这很有帮助)。我必须赞扬@scoffey,因为his solution以一种最终对我有意义的方式解析SOAP。在

编辑时间: 在Python中使用SOAP真的很有趣-大多数工具都多年没有维护过了。如果我们谈论特性,也许ZSI是领导者。但是如果它支持一些更复杂的XSD模式(仅举一个例子——它不支持基于扩展的联合和复杂类型,其中扩展类型不是基类型),那么它会有很多缺陷。 Suds非常容易使用,但不如ZSI强大——它对某些复杂的XSD结构的支持比ZSI差。 有一个有趣的工具generateDS,它可以使用XSD而不是直接与WSDL一起工作,您必须自己实现这些方法。但实际上它做得很好。在

相关问题 更多 >