簡體   English   中英

肥皂客戶端使用Suds

[英]Soap Client using Suds

用Python調用Soap

嗨,上面是我之前關於肥皂的問題。 在那里我傳遞一維數組。 現在我的問題是我需要將2D數組傳遞給以下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:Body>
    <CalculateWeb2DObjectArray xmlns="http://tempuri.org/">
      <HCID>string</HCID>
      <jaggedobjDataMICRO>
        <ArrayOfAnyType>
          <anyType />
          <anyType />
        </ArrayOfAnyType>
        <ArrayOfAnyType>
          <anyType />
          <anyType />
        </ArrayOfAnyType>
      </jaggedobjDataMICRO>
      <numeratorID>int</numeratorID>
    </CalculateWeb2DObjectArray>
  </soap:Body>
</soap:Envelope>

響應模式

 <?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:Body>
        <CalculateWeb2DObjectArrayResponse xmlns="http://tempuri.org/">
          <CalculateWeb2DObjectArrayResult>
            <ArrayOfAnyType>
              <anyType />
              <anyType />
            </ArrayOfAnyType>
            <ArrayOfAnyType>
              <anyType />
              <anyType />
            </ArrayOfAnyType>
          </CalculateWeb2DObjectArrayResult>
        </CalculateWeb2DObjectArrayResponse>
      </soap:Body>
    </soap:Envelope>

我的守則

from suds.xsd.doctor import Import, ImportDoctor
from suds.client import Client

# enable logging to see transmitted XML
import logging
logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.client').setLevel(logging.DEBUG)

# fix broken wsdl
# add <s:import namespace="http://www.w3.org/2001/XMLSchema"/> to the wsdl
imp = Import('http://www.w3.org/2001/XMLSchema',
             location='http://www.w3.org/2001/XMLSchema.xsd')
imp.filter.add('http://tempuri.org/')
wsdl_url = 'http://204.9.76.243/nuCast.DataFeedService/Service1.asmx?WSDL'
client = Client(wsdl_url, doctor=ImportDoctor(imp))

# make request
arrayofstring1 = client.factory.create('ArrayOfString')
arrayofstring1.string = [1,2]
arrayofstring2 = client.factory.create('ArrayOfString')
arrayofstring2.string = [5,6]
arrayofstring = client.factory.create('ArrayOfString')
arrayofstring.string = [arrayofstring1,arrayofstring2]


print client.service.CalculateWeb2DObjectArray(1073757, arrayofstring, 99)

但我在輸出中得到空值.Plz有助於解決這個問題。

謝謝

您將無效參數傳遞給CalculateWeb2DObjectArray()函數。

要找出CalculateWeb2DObjectArray()接受的參數類型,您可以添加到腳本中:

print client

輸出包含:

CalculateWeb2DObjectArray(xs:string HCID,
                          ArrayOfArrayOfAnyType jaggedobjDataMICRO,
                          xs:int numeratorID, )

所以第二個參數應該是ArrayOfArrayOfAnyType ,使用client.factory來創建它:

aoaoat = client.factory.create('ArrayOfArrayOfAnyType')

要了解如何填充aoaoat ,只需打印它:

print aoaoat

輸出:

(ArrayOfArrayOfAnyType){
  ArrayOfAnyType[] = <empty>
}

重復相同的ArrayOfAnyType過程,你會得到:

(ArrayOfAnyType){
  anyType[] = <empty>
}

把它們放在一起:

aoaoat = client.factory.create('ArrayOfArrayOfAnyType')
lst = aoaoat.ArrayOfAnyType = []
for L in [[1,2], [5,6]]:
    aoat = client.factory.create('ArrayOfAnyType')
    aoat.anyType = L
    lst.append(aoat)
response = client.service.CalculateWeb2DObjectArray(1073757, aoaoat, 99)
print response

請求

DEBUG:suds.client:sending to (
  http://204.9.76.243/nuCast.DataFeedService/Service1.asmx)
message:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:ns0="http://tempuri.org/"
  xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header/>
   <ns1:Body>
      <ns0:CalculateWeb2DObjectArray>
         <ns0:HCID>1073757</ns0:HCID>
         <ns0:jaggedobjDataMICRO>
            <ns0:ArrayOfAnyType>
               <ns0:anyType>1</ns0:anyType>
               <ns0:anyType>2</ns0:anyType>
            </ns0:ArrayOfAnyType>
            <ns0:ArrayOfAnyType>
               <ns0:anyType>5</ns0:anyType>
               <ns0:anyType>6</ns0:anyType>
            </ns0:ArrayOfAnyType>
         </ns0:jaggedobjDataMICRO>
         <ns0:numeratorID>99</ns0:numeratorID>
      </ns0:CalculateWeb2DObjectArray>
   </ns1:Body>
</SOAP-ENV:Envelope>
DEBUG:suds.client:headers = {
  'SOAPAction': u'"http://tempuri.org/CalculateWeb2DObjectArray"',
  'Content-Type': 'text/xml; charset=utf-8'}

響應

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <CalculateWeb2DObjectArrayResponse xmlns="http://tempuri.org/">
      <CalculateWeb2DObjectArrayResult>
        <ArrayOfAnyType>
          <anyType>1</anyType>
          <anyType>2</anyType>
        </ArrayOfAnyType>
        <ArrayOfAnyType>
          <anyType>5</anyType>
          <anyType>6</anyType>
        </ArrayOfAnyType>
      </CalculateWeb2DObjectArrayResult>
    </CalculateWeb2DObjectArrayResponse>
  </soap:Body>
</soap:Envelope>

產量

(ArrayOfArrayOfAnyType){
   ArrayOfAnyType[] = 
      (ArrayOfAnyType){
         anyType[] = 
            "1",
            "2",
      },
      (ArrayOfAnyType){
         anyType[] = 
            "5",
            "6",
      },
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM