python - 如何从 zeep python 库解析 soap Fault.detail(lxml.etree._Element)

标签 python soap zeep

当我使用 zeep 调用 soap 服务时,我正在尝试获取错误详细信息。

如何解析zeep.exceptions.Fault.detail?它返回 lxml.etree._Element。

我正在使用这段代码:

try:
    client = Client(wsdl=self.__wsdl)
    response = client.service.CustomerInformation(CustomerInformationService=self.service, faultStyle='wsdl')
except Fault as error:
    detail = error.detail
    # parse detail here

这是响应 XML:

<?xml version="1.0" ?>
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
    <soap-env:Body >
        <soap-env:Fault  >
            <faultcode>soap-env:Client</faultcode>
            <faultstring>Client Error</faultstring>
            <detail>
                <ouaf:Fault xmlns:ouaf="urn:oracle:ouaf">
                    <ResponseStatus>F</ResponseStatus>
                    <ResponseCode>2013</ResponseCode>
                    <ResponseText>
                            Error while executing the request:
                            (Server Message)
                                Category: 90006
                                Number: 32200
                                Call Sequence: 
                                Program Name: CustomerInformationService
                                Text: The personal account was not found: 9134211141
                                Description:  
                                Table: null
                                Field: null
                    </ResponseText>
                    <ResponseData numParm="1"  text="The personal account was not found: 9134211141"  category="90006"  number="32200"  parm1="9134211141"  />
                </ouaf:Fault>
            </detail>
        </soap-env:Fault>
    </soap-env:Body >
</soap-env:Envelope>

我的 wsdl 中存在来自 xml 数据的“故障”类型的区别。

最佳答案

我知道这是一个老问题,但寻找答案让我走到了这里,现在我也知道该怎么做了。

示例中 wsdl 的 URL 和凭据一起组成。

import zeep

url_to_wsdl = 'www.some_SOAP_site.com/soap?wsdl'

client = zeep.Client(url_to_wsdl)

credentials = {
    'login' : 'my_login',
    'pass' : 'my_pass'
}

my_query = "SELECT COLUMN1 FROM TABLE1"

try:
    client.service.query(my_query)
except zeep.exceptions.Fault as fault:
    parsed_fault_detail = client.wsdl.types.deserialize(fault.detail[0])

print(parsed_fault_detail)

结果

{
    'errorCode': 'INVALID_SESSION',
    'errorMessage': 'Invalid session!'
}

不要忘记 fault.detail 之后的 [0] 并尝试递增它以查看是否有更多错误详细信息。

关于python - 如何从 zeep python 库解析 soap Fault.detail(lxml.etree._Element),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42918753/

相关文章:

android - 使用 cxf 在 Android 设备上创建 web 服务?

python - 如何修复 SSL 问题 SSL_CTX_use_certificate : ca md too weak on Python Zeep

python - 如何修复 "zeep.exceptions.ValidationError: Missing element Value"

python - 如何在 django 中检查 BASE_DIR?

datetime - 如何清空 SOAP Web 服务中的日期时间字段?

java - 是否可以使用 Axis 2 wsdl2java 生成使用 HTTPS 的客户端代码?

Python、Zeep 对 Pandas 的回应

python - 在多列中用 np.nan 替换各种重复值

python - 如何使用 Python 轻松将变量扩展为字符串?

python - 如何在python中为套接字设置发送缓冲区大小