使用 SOAPpy 将标头部分添加到 SOAP 请求

发布于 2024-09-04 01:09:41 字数 1532 浏览 7 评论 0原文

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

I need to construct this SOAP query using python SOAPpy module:

<?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>

So I use this code:

from SOAPpy import WSDL

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

result = server.GetProductClassification();

The request generated is:

<?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>

When I send request I get Object reference not set to an instance of an object. I think this might be because I don't have a header section with license key in my request.

How do I modify my code to add header section with LicenseHeader parameter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧城烟雨 2024-09-11 01:09:41

我不知道如何在 SOAPpy 中执行此操作,但我确实知道如何在肥皂水中执行此操作。 SUDS 与 SOAPpy 做同样的事情,但它更新并且仍然受支持。我认为 SOAPpy 不再受支持。下面显示的是连接到 WSDL 并发送肥皂请求的代码:

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)

在发送肥皂请求之前将其放入脚本中,它将添加您想要的任何标头。

    # 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)

如果您使用类似的东西,您将能够添加任何标头、命名空间等。我已经使用过它并且它工作得很好。

要在 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)

I am not sure how to do this in SOAPpy but I do know how to do it in suds. SUDS does the same thing as SOAPpy but it is newer and is still supported. I don't think SOAPpy is supported anymore. Below show's the code to connect to a WSDL and send a soap request:

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)

Put this in the script before you send the soap request and it will add any headers you want.

    # 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)

This will allow you to add in wsa encding that makes the XML understand:

    # 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)

If you use something like this you will be able to add any headers, namespaces etc. I have used this and it worked perfectly.

To add the license header in SUDS add:

    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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文