如何将SOAP标头传递到WSDL文件中未定义的python SUDS中 [英] How to pass SOAP headers into python SUDS that are not defined in WSDL file

查看:256
本文介绍了如何将SOAP标头传递到WSDL文件中未定义的python SUDS中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网络上有一台摄像机,试图通过suds连接,但suds无法发送所需的所有信息.我需要放置WSDL文件中未定义的额外soap标头,以便摄像机可以理解该消息.所有标头都包含在SOAP信封中,然后suds命令应该在消息的正文中.

I have a camera on my network which I am trying to connect to with suds but suds doesn't send all the information needed. I need to put extra soap headers not defined in the WSDL file so the camera can understand the message. All the headers are contained in a SOAP envelope and then the suds command should be in the body of the message.

我已经检查了肥皂泡沫网站 它说要像这样传递标头:(这作为标头传递元素,但是我有一个信封,所以我不确定如何输入该标头)

I have checked the suds website and it says to pass in the headers like so: (This passes in the element as a header but I have an envelope so I'm not sure how to input this)

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)

现在,我不确定如何实现此目标.例如,我有以下标题:

Now, I am not sure how I would implement this. Say for example, I have the below header:

<?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消息传递给目标服务吗?

Using this or a similar example does anyone know how I would pass a valid SOAP message to the targeted service?

谢谢

推荐答案

我已经弄清楚了如何在suds中输入新的标题和名称空间. 如上所述,您可以创建一个Element并将其作为soapheader传递进来:

I have worked out how to enter in new headers and namespaces in suds. As stated above you create an Element and pass it in as a soapheader as so:

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.我不确定这是否可以达到预期目的,但是可以正常工作.

But if you would like to add a namespace I have found adding a prefix seem's to do the trick. So when you create one of the elements you add addPrefix. I'm not sure if this was the way it was intended to be done but it work's.

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

p = 'SOAP-ENC'可以是任何前缀eg. wsa,而u = http://address是命名空间的地址.

The p = 'SOAP-ENC' can be any prefix eg. wsa and the u = http://address is the address of the namespace.

可以运行的完整脚本可能是:

A complete script that would run could be:

#!/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()

这将移动相机.要更改肥皂信封的类型,请检查我的下一个问题.

This moves the camera. To change the type of soap-envelope check my next question.

您可以在该脚本中添加日志记录,允许您检查发送的xml命令是否方便:

You can add logging into this script whci allow's you to check what xml command you have sent which is handy:

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

如果该位置不在wsdl文件中,则可以将该位置作为选项放入脚本中.

The location can be put into the script as an option if the location is not in the wsdl file.

这篇关于如何将SOAP标头传递到WSDL文件中未定义的python SUDS中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆