SUDS v1.1.1触发‘Type not found’异常,求忽略未知字段方案
解决方法
你遇到的问题是SUDS默认的allowUnknownMessageParts选项对继承类型中的未知字段(比如这里paymentMethodsUsed返回的capitecPay类型包含基类未定义的accountNumber)不生效,以下是几种可行的解决办法:
1. 自定义SUDS插件拦截解析
通过插件修改WSDL的类型定义,允许任意未知字段:
from suds.plugin import DocumentPlugin class IgnoreUnknownTypesPlugin(DocumentPlugin): def parsed(self, context): # 遍历所有类型定义 types_node = context.root.childAtPath('types') if not types_node: return for complex_type in types_node.children: if complex_type.name == 'complexType': # 添加<any>元素,让SUDS跳过未知字段的解析 any_elem = complex_type.addChild('any') any_elem.set('minOccurs', '0') any_elem.set('processContents', 'skip')
初始化客户端时加载插件:
client = Client( spec_path, faults=False, plugins=[IgnoreUnknownTypesPlugin()] ) client.set_options(allowUnknownMessageParts=True, extraArgumentErrors=False)
2. 手动修改本地WSDL
直接在本地WSDL文件的capitecPay类型中添加缺失的accountNumber字段,比如:
<complexType name="capitecPay"> <!-- 原有字段 --> <element name="amountInCents" type="xsd:long"/> <!-- 添加缺失字段 --> <element name="accountNumber" type="xsd:string" minOccurs="0"/> </complexType>
之后用修改后的本地WSDL初始化客户端。
3. 绕过SUDS类型解析,直接处理原始XML
如果上述方法都无效,可以直接获取原始响应后自行解析:
# 调用接口 client.service.getTransaction(your_request_params) # 获取原始XML响应 raw_xml = client.last_received() # 用lxml解析XML from lxml import etree root = etree.fromstring(raw_xml) # 提取目标字段(注意替换成实际的命名空间) account_number = root.xpath( '//ns2:accountNumber', namespaces={'ns2': 'http://soap.api.controller.web.payjar.com/'} )[0].text
4. 切换SUDS分支版本
尝试使用SUDS-Jurko分支(这是SUDS的维护分支,对未知字段的处理更完善),安装后重新测试:
pip install suds-jurko
内容的提问来源于stack exchange,提问作者RunLoop
相关产品推荐
相关产品推荐

