通过 Zeep 创建具有可变数量的 XML 标记的 SOAP 请求



All,我正在使用Zeep连接到CUCM以执行批量AXL事务。我需要修改的一些对象接受可变数量的 XML 标记。例如:

我想添加一个实体(调用搜索空间),它可以有可变数量的分区与之关联。根据 WSDL:


    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.cisco.com/AXL/API/11.5">
       <soapenv:Header/>
       <soapenv:Body>
          <ns:addCss sequence="?">
             <css>
                <!--Optional:-->
                <description>?</description>
                <!--Optional:-->
                <members>
                   <!--Zero or more repetitions:--> <------------------------------------------
                   <member>
                      <routePartitionName uuid="?">?</routePartitionName>
                      <index>?</index>
                   </member>
                </members>
                <!--Optional:-->
                <partitionUsage>General</partitionUsage>
                <name>?</name>
             </css>
          </ns:addCss>
       </soapenv:Body>
    </soapenv:Envelope>

我可以轻松地编写固定数量的成员脚本:


    result = service.addCss({
        'name': 'SampleCSS',
        'description': 'Sample CSS Description',
        'members': {
            'member': [
                {'routePartitionName':{
                    '_value_1':None, 'uuid':'{07614566-ADC4-1ABB-3EB3-C08650E11CBE}'},
                    'index':1},
                {'routePartitionName': 'Auto Register','index': 2},
                {'routePartitionName': 'DNS External', 'index':3},
                {'routePartitionName':{
                    '_value_1':'On Cluster', 'uuid':'{F7FF933E-2B63-25DB-11AF-1DDDC8F9A52E}'},
                    'index':4},
            ]}
         })
    print (result)
    print(type(result))
    res = result['return']
    print("nn", res)

但是,我很难编写可变数量的成员的脚本

我把我的成员存储在字典上:


    CSSes = {
        'Test1': ['Test1','Test1','07614566-ADC4-1ABB-3EB3-C08650E11CBE','Staging',1],
        'Test2': ['Test2','Test2','F7FF933E-2B63-25DB-11AF-1DDDC8F9A52E', 'On Cluster',1]
    }

如果我添加多个 CSS 并且每个 CSS 都有不同数量的分区(成员),我该如何完成此操作?

我只能想出一些东西,其中每个CSS都分配了相同数量的分区:

for css in CSSes:
    result = service.addCss({
        'name': CSSes[css][0],
        'description': CSSes[css][1],
        'members': {
            'member': [
                {'routePartitionName':{
                        '_value_1':CSSes[css][3], 'uuid':CSSes[css][1]},
                        'index':CSSes[css][4]}
        ]}
    })

我不是 100% 确定我是否理解您的场景(主要基于提供的片段),但给定此数据集,其中一个要创建的 CSS 有 2 个分区,另一个有 3 个分区:

CSSs = [
    {
        'name': 'CSS1',
        'description': 'CSS1 description',
        'members': [
            {
                'name': 'Partition1',
                'index': 1
            },
            {
                'name': 'Partition2',
                'index': 2
            }
        ]
    },
    {
        'name': 'CSS2',
        'description': 'CSS2 description',
        'members': [
            {
                'name': 'Partition1',
                'index': 1
            },
            {
                'name': 'Partition2',
                'index': 2
            },
            {
                'name': 'Partition3',
                'index': 3
            }
        ]
    }
]

我能够通过动态附加成员来使其工作,如下所示:

for css in CSSs:
    css_data = {
        'name': css['name'],
        'description': css['description'],
        'members': {
            'member': []
        }
    }
    for memb in css['members']:
        css_data['members']['member'].append(
            {
                'routePartitionName': memb['name'],
                'index': memb['index']
            }
        )
    css_resp = service.addCss(css_data)

最新更新