如何在suds中添加工厂创建的类型作为标题?

时间:2010-11-05 16:36:44

标签: python soap header suds

我似乎无法使用我的设置。在使用API​​中的任何函数之前,我必须使用远程用户集传递上下文。我试图做的是:

client = Client(url, username=userid, password=password)
apiContext = client.factory.create("apiCallContext")     # This is listed in the types
apiContext.remoteUser = "serviceAccount"                 # when I print the client

client.set_options(soapheaders=apiContext)
client.service.getActiveIPs()

在整个过程中,一切似乎都被正确创建(如果我打印客户端,我看到所有的功能和类型,如果我打印apiContext,我看到所有设置都正确),但标题实际上似乎没有得到设定:

...
DEBUG:suds.client:sending to ($URL) message:
  <?xml version="1.0" encoding="UTF-8"?>
  <SOAP-ENV:Envelope xmlns:ns0=$NS 
                     xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/"
                     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                     xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <SOAP-ENV:Header/>
  <ns1:Body>
    <ns0:getActiveIPs/>
  </ns1:Body>
  </SOAP-ENV:Envelope>
DEBUG:suds.client:headers = {'SOAPAction': u'""', 
                             'Content-Type': 'text/xml; charset=utf-8'}
DEBUG:suds.transport.http:sending:
  URL:$URL
  HEADERS: {'SOAPAction': u'""', 'Content-Type': 'text/xml; charset=utf-8', 
            'Content-type': 'text/xml; charset=utf-8', 'Soapaction': u'""'}
...

我没有在标题中的任何位置看到上下文,并且服务器错误地说没有远程用户设置。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

在不知道您正在使用的网络服务的确切规格的情况下,我只能猜测这个答案,但标题看起来类似于我过去使用过的网络服务。您是否尝试直接创建元素并将其传递到标题中?:

from suds.sax.element import Element
...
NS = ('h', SOME_NAMESPACE)
apiContext = Element('apiContext')
authcred = Element('authenticationCredential', ns=NS)
username = Element(userid, ns=NS).setText('USERNAME')
passw = Element(password, ns=NS).setText('PASSWORD')
authcred.append(username)
authcred.append(passw)
apiContext.append(authcred)
client.set_options(soapheaders=apiContext)

即。是上下文对象的身份验证部分吗?