python - 使用 SOAPpy 将 header 部分添加到 SOAP 请求

标签 python xml soap soappy

我需要使用 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>

当我发送请求时,我得到对象引用未设置为对象的实例。我认为这可能是因为我没有带有许可证的header部分输入我的请求。

如何修改代码以添加带有 LicenseHeader 参数的 header 部分?

最佳答案

我不确定如何在 SOAPpy 中执行此操作,但我确实知道如何在 SOAP 水中执行此操作。 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 请求之前将其放入脚本中,它将添加您想要的任何 header 。

    # 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 编码,使 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)

如果你使用这样的东西,你将能够添加任何 header 、命名空间等。我已经使用了它并且它工作得很好。

要在 SUDS 中添加许可证 header ,请添加:

    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)

关于python - 使用 SOAPpy 将 header 部分添加到 SOAP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2964867/

相关文章:

python - 在 Ansible 中将 Jboss (WildFly) cli 输出转换为 json

python - Pyplot散点图干扰线

python - 删除 too 元素之间的多个 XML 元素

java - 如何使 photoview 库与 viewpager 一起工作

node.js - node-soap - 调用函数的正确方法

java - 由于 XML 属性顺序更改而导致数字签名损坏

python - Django 模型中的新字段未显示在管理界面或模型表单中

python - 如何让 GridSearchCV 在我的管道中使用自定义转换器?

python - 更紧凑的 ElementTree 或 lxml 命名空间

java - 使用 dataFormat 作为 POJO 通过 Camel 调用 Web 服务时无法设置 SOAP header