使用SOAPpy向SOAP请求添加头段

2024-10-01 00:24:50 发布

您现在位置:Python中文网/ 问答频道 /正文

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

所以我用这个代码:

^{pr2}$

生成的请求是:

<?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部分。在

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


Tags: orgenvhttpversionbodyxmlschemassoap
1条回答
网友
1楼 · 发布于 2024-10-01 00:24:50

我不知道怎么用肥皂水做这个,但我知道怎么用肥皂水。SUDS的功能与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请求之前将其放入脚本中,它将添加所需的任何头。在

^{pr2}$

这将允许您添加wsa encing,使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)

相关问题 更多 >