2010-03-18 4 views
13

네트워크에 suds로 연결하려고하는 카메라가 있지만 suds가 필요한 모든 정보를 보내지 않습니다. 카메라가 메시지를 이해할 수 있도록 WSDL 파일에 정의되지 않은 여분의 비누 헤더를 넣어야합니다. 모든 헤더는 SOAP 봉투에 들어 있으며 suds 명령은 메시지 본문에 있어야합니다.SOAP 헤더를 파이썬에 전달하는 방법 WSDL 파일에 정의되지 않은 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) 

자, 어떻게 구현할지 모르겠습니다. 이 또는 유사한 예는 사람이 내가 대상 서비스에 유효한 SOAP 메시지를 전달 할 방법을 알고 않습니다를 사용

<?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> 

: 예를 들어 말해, 나는 아래의 헤더가?

감사합니다.

답변

20

새 머리글과 네임 스페이스를 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) 

을하지만 당신은 내가 접두사를 추가 발견 한 네임 스페이스를 추가 할 경우 트릭을 할의를 보인다. 따라서 요소 중 하나를 만들 때 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. 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() 

이 카메라를 이동합니다. 비누 봉투 검사 유형을 변경하려면 my next question.

당신은 당신이 XML 명령을 사용하면 편리하다 보낸 것을 확인의 허용 WHCI이 스크립트에 로깅을 추가 할 수 있습니다

import logging 
logging.basicConfig(level=logging.INFO) 
logging.getLogger('suds.client').setLevel(logging.DEBUG) 
위치가 아닌 경우 위치는 옵션으로 스크립트에 넣을 수

wsdl 파일.

관련 문제