2015-06-16 47 views
1

我需要調用一個SOAP服務,這樣的消息:如何用Python suds庫創建肥皂標題?

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sub="https://secure.xpslogic.com/service/wijnen/sub"> 
    <soapenv:Header> 
     <sub:auth> 
     <token>?</token> 
     <!--Optional:--> 
     <user_id>?</user_id> 
     <!--Optional:--> 
     <user_token>?</user_token> 
     </sub:auth> 
    </soapenv:Header> 
    <soapenv:Body> 
     <sub:customer_logos_pull> 
     <!--Optional:--> 
     <language>?</language> 
     <!--Optional:--> 
     <limit>?</limit> 
     <!--Optional:--> 
     <options_utc>?</options_utc> 
     </sub:customer_logos_pull> 
    </soapenv:Body> 
</soapenv:Envelope> 

我有一些PHP代碼示例,這臺頭如下(並且工作得很好):

AUTH = array(); $ auth ['token'] ='xxx'; 如果($ AUTH){// 添加AUTH頭 $這 - >客戶端[$模塊] - > __ setSoapHeaders( 新SOAPHEADER( $命名空間, 'AUTH', $ AUTH ) ); }

我現在構建(空)正文和信頭與Python suds lib如下:

from suds.client import Client 
from suds import WebFault 

client = Client(url='https://example.com/sub.wsdl') 

auth = client.factory.create('auth') 
auth.token = 'xxx' 
client.set_options(soapheaders=auth) 

customerLogosPull = client.factory.create('customer_logos_pull') 
result = client.service.customer_logos_pull(customerLogosPull) 

但是這給我一個not well-formed (invalid token)消息。登錄時打開我覺得這是消息:

DEBUG:suds.mx.core:processing: 
(Content){ 
    tag = "auth" 
    value = 
     (auth){ 
     token = "xxx" 
     user_id = None 
     user_token = None 
     } 
    type = <Element:0x10ff8c950 name="auth"> 
    <Complex:0x10ff8cbd0> 
     <Sequence:0x10ff8cc50> 
     <Element:0x10ff8cd10 name="token" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" /> 
     <Element:0x10ff8cd50 name="user_id" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" /> 
     <Element:0x10ff8cd90 name="user_token" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" /> 
     </Sequence> 
    </Complex> 
</Element> 
} 

它看起來由我很精細,但它也給一個not well-formed (invalid token)。眼見suds docs has 3 examples如何在SOAP頭傳遞,我嘗試了其他兩種以及:

>>> token = client.factory.create('auth.token') 
>>> token.set(TOKEN) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
AttributeError: token instance has no attribute 'set' 

>>> client.set_options(soapheaders={'auth': {'token': 'xxx'}}) 
>>> customerLogosPull = client.factory.create('customer_logos_pull') 
>>> result = client.service.customer_logos_pull(customerLogosPull) 

這給在日誌中這樣的內容,仍然是一個not well-formed (invalid token)

(Content){ 
    tag = "auth" 
    value = 
     { 
     token = "xxx" 
     } 
    type = <Element:0x106049290 name="auth"> 
    <Complex:0x106049510> 
     <Sequence:0x106049590> 
     <Element:0x106049650 name="token" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" /> 
     <Element:0x106049690 name="user_id" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" /> 
     <Element:0x1060496d0 name="user_token" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" /> 
     </Sequence> 
    </Complex> 
</Element> 
} 

有沒有人知道我可以如何使用Python在標頭中正確設置標記?歡迎所有提示!

+0

如何發你有記錄爲你格式化日誌嗎? – JohnnyQ

回答

2

我得到了我的SOAP頭如下工作:對應於SOAP請求XML如下所示的SOAP頭

from suds.sax.element import Element 
ssnp = Element("xsi:SessionHeader").append(Element('xsi:sessionId').setText("xxxxxxx")) 
client.set_options(soapheaders=ssnp) 

<SOAP-ENV:Header> 
    <xsi:SessionHeader> 
    <xsi:sessionId>xxxxxxx</xsi:sessionId> 
    </xsi:SessionHeader> 
</SOAP-ENV:Header> 

我們可以看到請求使用print client.last_sent()