使用 Python Zeep 自省 WSDL

Introspecting a WSDL with Python Zeep

我正在尝试使用 Zeep 来描述给定 WSDL 中的操作和类型,以便程序知道操作名称、它们的参数名称、参数类型和参数属性。

此信息将用于为给定的 WSDL 动态生成 UI。

到目前为止我得到的只是操作和类型的字符串表示。使用类似于 .

中的代码

这是一个例子:

from zeep import Client
import operator

wsdl = 'http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl'
client = Client(wsdl)

# get each operation signature
for service in client.wsdl.services.values():
    print("service:", service.name)
    for port in service.ports.values():
        operations = sorted(
            port.binding._operations.values(),
            key=operator.attrgetter('name'))

        for operation in operations:
            print("method :", operation.name)
            print("  input :", operation.input.signature())
            print()
    print()

# get a specific type signature by name
complextype = client.get_type('ns0:CartGetRequest')
print(complextype.name)
print(complextype.signature())

这给出了如下输出(为简洁起见缩短)

[...]

method : CartCreate
  input : MarketplaceDomain: xsd:string, AWSAccessKeyId: xsd:string, AssociateTag: xsd:string, Validate: xsd:string, XMLEscaping: xsd:string, Shared: ns0:CartCreateRequest, Request: ns0:CartCreateRequest[]

method : CartGet
  input : MarketplaceDomain: xsd:string, AWSAccessKeyId: xsd:string, AssociateTag: xsd:string, Validate: xsd:string, XMLEscaping: xsd:string, Shared: ns0:CartGetRequest, Request: ns0:CartGetRequest[]

[...]


CartGetRequest
{http://webservices.amazon.com/AWSECommerceService/2011-08-01}CartGetRequest(CartId: xsd:string, HMAC: xsd:string, MergeCart: xsd:string, ResponseGroup: xsd:string[])

.signature() 返回的字符串表示具有名称和类型,但我不知道如何将它们单独解析出来。我也尝试过使用 dir() 遍历每个对象属性,但它们不包含此信息。好像嵌套的更深了。

我可以自己解析字符串表示,但是我也不知道参数是否是可选的(更具体地说,如果它具有属性 minOccurs=0

好像是SOAPpy actually has this functionality,但是已经不维护了

那么有没有一种方法可以使用 zeep 来内省 WSDL,它提供关于每个操作的详细信息,它是类似于 SOAPpy 实现的参数名称、类型和属性?或者我应该解析签名,或者使用常规 XML 解析器解析 WSDL。

您可以使用 operation.input.body.type.elements 访问参数元素,这是包含元素对象的元组列表。这些对象包含类型等信息。

(Pdb) operation.input.body.type.elements
[('MarketplaceDomain', <Element(name='MarketplaceDomain', type=<zeep.xsd.types.builtins.String object at 0x7f1bd8a4b320>)>), ('AWSAccessKeyId', <Element(name='AWSAccessKeyId', type=<zeep.xsd.types.builtins.String object at 0x7f1bd8a4b320>)>), ('AssociateTag', <Element(name='AssociateTag', type=<zeep.xsd.types.builtins.String object at 0x7f1bd8a4b320>)>), ('Validate', <Element(name='Validate', type=<zeep.xsd.types.builtins.String object at 0x7f1bd8a4b320>)>), ('XMLEscaping', <Element(name='XMLEscaping', type=<zeep.xsd.types.builtins.String object at 0x7f1bd8a4b320>)>), ('Shared', <Element(name='Shared', type=<zeep.xsd.dynamic_types.BrowseNodeLookupRequest object at 0x7f1bd8177e48>)>), ('Request', <Element(name='Request', type=<zeep.xsd.dynamic_types.BrowseNodeLookupRequest object at 0x7f1bd8177e48>)>)]
(Pdb) operation.input.body.type.elements[0][1].name
'MarketplaceDomain'
(Pdb) operation.input.body.type.elements[0][1].type.name
'string'
(Pdb) operation.input.body.type.elements[0][1].is_optional
True

根据 jordanm 的回答,我使用以下方法获取可用方法所需的所有数据

from zeep import Client
from pprint import pprint

wsdl = 'http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl'
client = Client(wsdl)


def parseElements(elements):
    all_elements = {}
    for name, element in elements:
        all_elements[name] = {}
        all_elements[name]['optional'] = element.is_optional
        if hasattr(element.type, 'elements'):
            all_elements[name]['type'] = parseElements(
                element.type.elements)
        else:
            all_elements[name]['type'] = str(element.type)

    return all_elements


interface = {}
for service in client.wsdl.services.values():
    interface[service.name] = {}
    for port in service.ports.values():
        interface[service.name][port.name] = {}
        operations = {}
        for operation in port.binding._operations.values():
            operations[operation.name] = {}
            operations[operation.name]['input'] = {}
            elements = operation.input.body.type.elements
            operations[operation.name]['input'] = parseElements(elements)
        interface[service.name][port.name]['operations'] = operations


pprint(interface)