-->

添加页眉部分使用SOAPpy的SOAP请求(Add Header section to SOAP r

2019-10-22 15:36发布

我需要使用Python SOAPpy的模块来构建这个SOAP查询:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
 <soap:Header>
  <LicenseHeader xmlns="http://schemas.acme.eu/">
   <LicenseKey>88888-88888-8888-8888-888888888888</LicenseKey>
  </LicenseHeader>
 </soap:Header>
 <soap:Body>
  <GetProductClassification xmlns="http://schemas.acme.eu/">
   <GetProductClassificationRequest />
  </GetProductClassification>
 </soap:Body>
</soap:Envelope>

所以我用这个代码:

from SOAPpy import WSDL

wsdlFile = 'https://example.comt/1.0/service.asmx?wsdl'
server = WSDL.Proxy(wsdlFile)

result = server.GetProductClassification();

所产生的要求是:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
  SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
  xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
>
<SOAP-ENV:Body>
<GetProductClassification SOAP-ENC:root="1">
</GetProductClassification>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

当我把我的请求得到Object reference not set to an instance of an object. 我想这可能是因为我没有一个header ,在我的要求许可密钥部分。

如何修改我的代码添加header部分与LicenseHeader参数?

Answer 1:

我不知道如何在SOAPpy的做到这一点,但我知道如何做到这一点的泡沫。 SUDS做同样的事情SOAPpy的,但它是新的,现在仍然支持。 我不认为是SOAPpy中不再支持。 下面展示的连接到WSDL和发送SOAP请求的代码:

class MySudsClass():

def sudsFunction(self):

    url = "http://10.10.10.10/mywsdl.wsdl"

    # connects to WSDL file and stores location in variable 'client'
    client = Client(url)

    #I have no address set in the wsdl to the camera I connect to so I set it's location here
    client.options.location = 'http:/10.10.10.11'

    # Create 'xml_value' object to pass as an argument using the 'factory' namespace
    xml_value = client.factory.create('some_value_in_your_xml_body')

    #This send the SOAP request.
    client.service.WSDLFunction(xml_value)

将这个脚本您发送SOAP请求之前,它会添加任何你想要的标题。

    # Namespaces to be added to XML sent 
    wsa_ns = ('wsa', 'http://schemas.xmlsoap.org/ws/2004/08/addressing')
    wsdp_ns = ('http://schemas.xmlsoap.orf/ws/2006/02/devprof')

    # Field information for extra XML headers
    message = 'mymessage'
    address_txt = 'myheader_information'

    # Soapheaders to be added to the XML code sent
    # addPrefix allow's you to addc a extra namespace. If not needed remove it.
    message_header = Element('MessageID', ns=wsa_ns).setText(message)
    address_header = Element('Address', ns=wsa_ns).setText(address_txt).addPrefix(p='wsdp', u=wsdp_ns) 

    header_list = [message_header, address_header]

    # Soapheaders being added to suds command
    client.set_options(soapheaders=header_list)

这将允许您在WSA encding,使XML理解补充:

    # Attribute to be added to the headers to make sure camera verifies information as correct
    mustAttribute = Attribute('SOAP-ENV:mustUnderstand', 'true')
    for x in header_list:
        x.append(mustAttribute)

如果您使用的东西,这样你就可以添加任何头,命名空间等等。我已经使用这个它完美地工作。

要在SUDS添加许可证头添加:

    license_key = Element('LicenseKey', ns=some_namespace).setText('88888-88888-8888-8888-888888888888')
    license_header = Element('LicenseHeader', ns=some_namespace).insert(license_key)

    license_attribute = Attribute(xmlns, "http://schemas.acme.eu/")
    license_header.append(license_attribute)


文章来源: Add Header section to SOAP request using SOAPpy