NNMi 10.20 SOAP上的多个WS-I过滤器

时间:2018-11-20 08:51:33

标签: python soap zeep

我想在NNMi 10.20 SOAP上应用多个WS-I过滤器,我使用Zeep Python库:

from requests.auth import HTTPBasicAuth  # or HTTPDigestAuth, or OAuth1
from requests import Session
from zeep import Client
from zeep.transports import Transport

user = 'admin'
password = 'secret'
url = 'http://domaine.fr/NodeBeanService/NodeBean?wsdl'
node_id= "144077343434" 

session = Session()
session.auth = HTTPBasicAuth(user, password)
client = Client(url, transport=Transport(session=session))


factory = client.type_factory('ns3')
constraint = factory.constraint(name="includeCustomAttributes", value=1)
condition = factory.condition(name="id", operator="EQ", value=node_id)
filterr = factory.expression(operator="AND", subFilters=[constraint, condition])
node_infos = client.service.getNodes(filterr)

我收到此错误:

Fault: java.lang.IllegalArgumentException: prefix ns1 is not bound to a namespace

请问解决办法。

提前谢谢。

1 个答案:

答案 0 :(得分:0)

这是XML的请求, 我用SOAPUI发送它

  <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
    <env:Body>
        <ns1:getNodes xmlns:ns1="http://node.sdk.nms.ov.hp.com/">
            <arg0 xsi:type="ns3:expression" xmlns:ns3="http://filter.sdk.nms.ov.hp.com/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
               <operator>AND</operator>
               <subFilters xsi:type="ns3:condition">
                   <name>id</name>
                   <operator>EQ</operator>
                   <value>34345454656</value>
               </subFilters>
               <subFilters xsi:type="ns3:constraint">
                   <name>includeCustomAttributes</name>
                       <value>true</value>
               </subFilters>
            </arg0>
        </ns1:getNodes>
    </env:Body>
</env:Envelope>

您可以使用请求来完成

from lxml import etree
import requests 

from config import config, urls

def prepare_xml_request(deviceId, includeCustomAttributes="true"):

    return """   
        <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
            etc ......
                           <name>id</name>
                           <operator>EQ</operator>
                           <value>{0}</value>
                       </subFilters>
                       <subFilters xsi:type="ns3:constraint">
                           <name>includeCustomAttributes</name>
                               <value>{1}</value>
                       </subFilters>

        </env:Envelope>
        """.format(deviceId, includeCustomAttributes)

def parse_response(resp_file):
    tree = etree.parse(resp_file)
    root = tree.getroot()
    customAttributes = {}
    hostname = ""
    for customAttribut in root.iter("customAttributes"):
        att = []
        for element in customAttribut.iter("*"):
            if element.tag != 'customAttributes':
                att.append(element.text)

        customAttributes[att[0]] = att[1]

    for longName in root.iter("longName"):
        hostname = longName.text

    return (hostname, customAttributes)

def get_infos_node(node_id):
    xml_req = prepare_xml_request(node_id)

    resp = requests.post(urls.get('Node'), data=xml_req, auth=(config.get('username'), config.get('password'))).text

    f = open('node_resp.xml', mode='w', encoding='utf-8')
    f.write(resp)
    f.close()

    # parser le resp et recuper le hostname et les customattributes
    (hostname, customAttributes) = parse_response('node_resp.xml')

    # return hostname, customAttributes
    return (hostname, customAttributes)