簡體   English   中英

如何使用SUDS python包編寫基於SOAP的Web服務腳本

[英]How to script SOAP based web service using SUDS python package

我想使用python使用SUDS包自動化基於SOAP的Web服務。 我在編寫腳本入門時遇到問題。 既然,我們使用的Web服務包含多個標簽,那么復雜的結構可能會有人幫助我創建一個。 以下是Web服務的xml請求。

XML請求:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:mod="http://models.ws.common.abc.com" xmlns:ser="http://sa.models.ws.common.abc.com">
   <soapenv:Header>
      <mod:adtwsHeader>
         <adtwss>
            <userName>admin</userName>
            <password>password</password>
         </adtwss>
      </mod:adtwsHeader>
   </soapenv:Header>
   <soapenv:Body>
      <ser:ServiceRequest>
         <subId>Test_DLT</subId>
      </ser:ServiceRequest>
   </soapenv:Body>
</soapenv:Envelope>

我可以直到這里完成腳本:

from suds.client import Client
from suds.wsse import *
#URL invocation
url = 'WDSL_URL'
client = Client(url)

#print client

result = client.service.retrieveService(subId='Test_DLT')
print result

以下是我運行腳本Test_DLT時的輸出

它正在控制台上自己打印subid。 但是實際上,它應該從數據庫中檢索相應子ID的服務信息作為響應。 我們的Web服務需要在xml請求中提供身份驗證(用戶名,密碼)。 我遇到了使用SUDS對該部分進行編碼的問題。這是我們提供用戶名和密碼的標頭部分:

<soapenv:Header>
      <mod:adtwsHeader>
         <adtwss>
            <userName>admin</userName>
            <password>password</password>
         </adtwss>
      </mod:adtwsHeader>
   </soapenv:Header>

有人可以幫助我還是為我提供上述代碼的SUDS代碼。

感謝您的任何幫助。 -薩沙

suds文檔中有一小節關於自定義標頭。 我無法對其進行測試,但是這樣的方法可能有效:

from suds.sax.element import Element

ns_mod = ('mod', 'http://models.ws.common.abc.com')

header = Element('adtwsHeader', ns=ns_mod)
container = Element('adtwss', parent=header)

username = Element('userName', parent=container)
username.setText('usernameGoesHere')

password = Element('password', parent=container)
password.setText('myPassword123')

client.set_options(soapheaders=header) 

暫無
暫無

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

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