2010-03-18 55 views
13

我在我的網絡上有一個攝像頭,我試圖用suds連接,但是suds不發送所有需要的信息。我需要在WSDL文件中添加未定義的額外SOAP標題,以便相機可以理解消息。所有標題都包含在SOAP信封中,然後suds命令應該位於消息的正文中。如何將SOAP頭傳入未定義在WSDL文件中的python SUDS

我已經檢查了泡沫website 和它說,在像這樣的標題來傳遞:(這通過在元素作爲一個頭,但我有一個信封,所以我不知道如何輸入這個)

from suds.sax.element import Element 
client = client(url) 
ssnns = ('ssn', 'http://namespaces/sessionid') 
ssn = Element('SessionID', ns=ssnns).setText('123') 
client.set_options(soapheaders=ssn) 
result = client.service.addPerson(person) 

現在,我不知道我將如何實現這一點。比方說,我有以下標題:

<?xml version="1.0" encoding="UTF-8"?> 
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" xmlns:SOAP 
ENC="http://www.w3.org/2003/05/soap-encoding" 
<wsa:MessageID SOAP-ENV:mustUnderstand="true">urn:uuid:43268c01-f09c6</wsa:MessageID> 
<SOAP-ENV:Header> 

使用這種或類似的例子,沒有人知道我怎麼會傳遞一個有效的SOAP消息發送到目標服務?

謝謝

回答

20

我已經制定了如何在泡沫中輸入新的標題和命名空間。 如上所述你創建一個元素,並把它作爲一個SOAPHEADER像這樣:

from suds.sax.element import Element 
client = client(url) 
ssnns = ('ssn', 'http://namespaces/sessionid') 
ssn = Element('SessionID', ns=ssnns).setText('123') 
client.set_options(soapheaders=ssn) 
result = client.service.addPerson(person) 

但是,如果你想添加一個命名空間,我發現添加前綴似乎對這樣的伎倆。所以當你創建一個你添加的元素addPrefix。我不確定這是否是它打算完成的方式,但它的工作。

ssn = Element('SessionID', ns=ssnns).setText('123').addPrefix(p='SOAP-ENC', u='http://www.w3.org/2003/05/soap-encoding') 

p = 'SOAP-ENC'可以是任何前綴eg. wsau = http://address是命名空間的地址。

將運行

一個完整的腳本可能是:

#!/usr/local/bin/python2.6 

import suds 
#import logging 
from suds.client import Client 
from suds.sax.element import Element 
from suds.sax.attribute import Attribute 
from suds.xsd.sxbasic import Import 

def absoluteMove(): 

    # connects to WSDL file and stores location in variable 'client' 
    client = Client('http://10.10.10.10/p.wsdl') 
    client.options.location = 'http://10.10.10.10:32963' 

    # Create the header 
    wsans = ('wsa', 'http://schemas.xmlsoap.org/ws/2004/08/addressing') 
    mustAttribute = Attribute('SOAP-ENV:mustUnderstand', 'true') 
    n1s = ('SOAP-ENC', 'http://www.w3.org/2003/05/soap-encoding') 
    msgId = Element('Element').addPrefix(p='SOAP-ENC', u='http://www.w3.org/2003/05/soap-encoding') 

    msgId2 = Element('Address', ns=wsans).setText('http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous') 
    msgId1 = Element('ReplyTo', ns=wsans).insert(msgId2) 
    msgId1.append(mustAttribute) 

    msgId3 = Element('To', ns=wsans).setText('http://10.10.10.10:32954') 
    msgId3.append(mustAttribute) 

    client.set_options(soapheaders=[msgId, msgId1, msgId3, msgId2]) 

    # Create 'token' object to pass as an argument using the 'factory' namespace 
    token = client.factory.create('ns4:ReferenceToken') 

    # Create 'dest' object to pass as an argument and values passed to this object 
    dest = client.factory.create('ns4:PTZVector') 
    dest.PanTilt._x=1 
    dest.PanTilt._y=4.9 
    dest.Zoom._x=1 


    # Create 'speed' object to pass as an argument and values passed to this object 
    speed = client.factory.create('ns4:PTZSpeed') 
    speed.PanTilt._x=0 
    speed.PanTilt._y=0 
    speed.Zoom._x=1 

    # 'AbsoluteMove' method invoked passing in the new values entered in the above objects 

    try: 
     result = client.service.AbsoluteMove(token, dest, speed) 
     print "absoluteMove result ", result 
     return result 
    except suds.WebFault, e: 
     print "suds.WebFaults caught: " 
     print e 

if __name__ == '__main__': result = absoluteMove() 

該移動相機。要更改肥皂信封的類型,請檢查my next question

您可以添加記錄到這個腳本WHCI允許的,你檢查什麼XML命令已發送這是很方便:

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

位置可以被放入腳本作爲一個選項,如果該位置不在wsdl文件。

相關問題