chr*_*ley 14 python xml wsdl suds soapheader
我的网络上有一个摄像头,我试图用肥皂水连接,但泡沫不会发送所需的所有信息.我需要在WSDL文件中放置未定义的额外soap标头,以便摄像头可以理解该消息.所有头文件都包含在SOAP信封中,然后suds命令应该在邮件正文中.
我已经检查了suds 网站 ,它说要像这样传入标题:(这会将元素作为标题传递,但我有一个信封,所以我不知道如何输入这个)
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)
Run Code Online (Sandbox Code Playgroud)
现在,我不确定如何实现这一点.比方说,我有以下标题:
<?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>
Run Code Online (Sandbox Code Playgroud)
使用这个或类似的例子有谁知道如何将有效的SOAP消息传递给目标服务?
谢谢
chr*_*ley 21
我已经研究了如何在suds中输入新的头文件和命名空间.如上所述,您创建一个元素并将其作为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)
Run Code Online (Sandbox Code Playgroud)
但是,如果你想添加一个命名空间,我发现添加一个前缀似乎是为了做到这一点.因此,当您创建添加的元素之一时addPrefix.我不确定这是否是它打算完成的方式,但它有效.
ssn = Element('SessionID', ns=ssnns).setText('123').addPrefix(p='SOAP-ENC', u='http://www.w3.org/2003/05/soap-encoding')
Run Code Online (Sandbox Code Playgroud)
该p = 'SOAP-ENC'可以是任何前缀eg. wsa和u = 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()
Run Code Online (Sandbox Code Playgroud)
这会移动相机.要更改肥皂信封的类型,请检查我的下一个问题.
您可以在此脚本中添加日志记录,以便您检查已发送的xml命令,这很方便:
import logging
logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.client').setLevel(logging.DEBUG)
Run Code Online (Sandbox Code Playgroud)
如果位置不在wsdl文件中,则可以将该位置作为选项放入脚本中.
| 归档时间: |
|
| 查看次数: |
19171 次 |
| 最近记录: |