【问题标题】:Python SOAP to code converter?Python SOAP 到代码转换器?
【发布时间】:2015-07-27 14:21:35
【问题描述】:

我需要发送一个看起来完全像这样的SOAP envelope...

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:realops.com:amp:workflow" xmlns:oas="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:dst="DST_JC_Sandbox">
    <soapenv:Header>
      <urn:grid-name>GRID01</urn:grid-name>
      <oas:Security>
         <!--Optional:-->
         <oas:UsernameToken>
            <oas:Username>Network</oas:Username>
            <oas:Password>password</oas:Password>
         </oas:UsernameToken>
      </oas:Security>
   </soapenv:Header>
   <soapenv:Body>
      <dst:TrySoap-Request>
         <dst:strInput1>abc</dst:strInput1>
         <dst:strInput2>abc123</dst:strInput2>
      </dst:TrySoap-Request>
   </soapenv:Body>
</soapenv:Envelope>

我正在使用 Python suds。但是,我只是不关心如何控制标题。由于我仍在尝试发现如何将成功的 SOAP 调用发送到我的内部测试主机(它确实响应soapUI),所以我只得到了一些被破解的部分。

from suds.client import *
from suds.transport.http import HttpAuthenticated
from suds.wsse import *
from suds.xsd.sxbasic import Import
from suds.sax.element import Element    

def main():

logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.client').setLevel(logging.DEBUG)
logging.getLogger('suds.transport').setLevel(logging.DEBUG)

url = 'http://10.1.1.1:8080/path/name/wsdl?grid-name=GRID01'
client = Client(url, faults=False)
tag_name1 = ('urn', url)
urn = Element('grid-name', ns=tag_name1).setText('GRID01')
client.set_options(soapheaders=urn)
print client.service.TrySoap('abc','123')

这会产生一个像这样的 SOAP 信封...

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="Sandbox" xmlns:urn="http://10.1.1.1:8080/path/name/wsdl?grid-name=GRID01" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header>
      <urn:grid-name>GRID01</urn:grid-name>
   </SOAP-ENV:Header>
   <ns0:Body>
      <ns1:TrySoap-Request>
         <ns1:strInput1>abc</ns1:strInput1>
         <ns1:strInput2>xyz</ns1:strInput2>
      </ns1:TrySoap-Request>
   </ns0:Body>
</SOAP-ENV:Envelope>

我对其工作原理的理解程度只是我发送的信封必须与我发布的第一个样本完全相同。此请求不起作用,服务器响应“DEBUG:suds.client:http failed:”

我知道我的python generated header doesn't match

问题:

  1. 我的 URL 是否应该显示在标题中?
  2. 如何在 UsernameToken 标记中嵌套用户名和密码?
  3. 我是否正确构造了urn 元素?还是有更好的方法?
  4. 如何在标题中获取xmlns:urn="urn:realops.com:amp:workflow"
  5. 是否有 conversion tool 可以在其中输入格式化的信封并让它吐出 Python 形成该信封所需的代码?

【问题讨论】:

    标签: python web-services soap suds


    【解决方案1】:

    一位同事为我提供了此测试电话的解决方案...

    from suds.client import Client
    from suds.wsse import *
    import logging
    
    logging.basicConfig(level=logging.INFO)
    logging.getLogger('suds.client').setLevel(logging.DEBUG)
    logging.getLogger('suds.transport').setLevel(logging.DEBUG)
    
    url = 'http://10.1.1.1:8080/file/path/wsdl?grid-name=GRID01'
    client = Client(url)
    security = Security()
    token = UsernameToken('JoeBob','password')
    security.tokens.append(token)
    client.set_options(wsse=security)
    gridns = ('tns','urn:realops.com:amp:workflow')
    grid = Element('grid-name',ns=gridns).setText('GRID01')
    client.set_options(soapheaders=grid)
    result = client.service.TrySoap('abc','123')
    print result
    

    这产生了这个信封。

    <?xml version="1.0" encoding="UTF-8"?>
    <SOAP-ENV:Envelope xmlns:tns="urn:realops.com:amp:workflow" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="Sandbox" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
       <SOAP-ENV:Header>
          <wsse:Security mustUnderstand="true">
             <wsse:UsernameToken>
                <wsse:Username>JoeBob</wsse:Username>
                <wsse:Password>password</wsse:Password>
             </wsse:UsernameToken>
          </wsse:Security>
          <tns:grid-name>GRID01</tns:grid-name>
       </SOAP-ENV:Header>
       <ns0:Body>
          <ns1:TrySoap-Request>
             <ns1:strInput1>abc</ns1:strInput1>
             <ns1:strInput2>123</ns1:strInput2>
          </ns1:TrySoap-Request>
       </ns0:Body>
    </SOAP-ENV:Envelope>
    

    这行得通,但我想了解为什么要好一点。我对标签的重要性感到困惑:变量格式。我也不太了解命名空间是如何通过 Element 调用更改的。

    【讨论】:

      猜你喜欢
      • 2012-07-25
      • 1970-01-01
      • 1970-01-01
      • 2014-01-23
      • 2013-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多