本文介绍了如何用Python suds库创建肥皂标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < soapenv:Envelope xmlns:soapenv = http://schemas.xmlsoap.org/soap/envelope/xmlns:sub =https://secure.xpslogic.com/service/wijnen/sub> 
< soapenv:Header>
< sub:auth>
< token>?< / token>
<! - 可选: - >
< user_id>?< / user_id>
<! - 可选: - >
< user_token>?< / user_token>
< / sub:auth>
< / soapenv:Header>
< soapenv:Body>
< sub:customer_logos_pull>
<! - 可选: - >
< language>?< / language>
<! - 可选: - >
< limit>?< / limit>
<! - 可选: - >
< options_utc>?< / options_utc>
< / sub:customer_logos_pull>
< / soapenv:Body>
< / soapenv:Envelope>

我有一些php示例代码,它将头文件设置为如下所示):

auth = array();
$ auth ['token'] ='xxx';
if($ auth){
// add auth header
$ this-> clients [$ module] - > __ setSoapHeaders(
new SoapHeader(
$ namespace,
'auth',
$ auth

);
}



我现在使用:来自suds.client的

  import Client 
from suds import WebFault

client = Client(url ='https://example.com/sub.wsdl')

auth = client.factory.create('auth' )
auth.token ='xxx'
client.set_options(soapheaders = auth)
$ b $ customerLogosPull = client.factory.create('customer_logos_pull')
result = client.service.customer_logos_pull(customerLogosPull)

但这给我一个不好形成(无效令牌)消息。打开日志记录时,我发现这是消息:

  DEBUG:suds.mx.core:processing:
(内容){
tag =auth
值=
(auth){
令牌=xxx
user_id =无
user_token =无
}
type =<元素:0x10ff8c950 name =auth>
< Complex:0x10ff8cbd0>
<序列:0x10ff8cc50>
< Element:0x10ff8cd10 name =tokentype =(u'string',u'http://www.w3.org/2001/XMLSchema')/>
< Element:0x10ff8cd50 name =user_idtype =(u'string',u'http://www.w3.org/2001/XMLSchema')/>
< Element:0x10ff8cd90 name =user_tokentype =(u'string',u'http://www.w3.org/2001/XMLSchema')/>
< / Sequence>
< / Complex>
< / Element>
}

我看起来很好,但它也给出了一个格式不正确(无效令牌)。看到关于如何传递肥皂标题,我尝试了其他两个:

 >>> token = client.factory.create('auth.token')
>>> token.set(TOKEN)
Traceback(最近一次调用的最后一个):
在< module>中,第1行的文件< stdin>
AttributeError:令牌实例没有属性'set'

 >>> client.set_options(soapheaders = {'auth':{'token':'xxx'}})
>>> customerLogosPull = client.factory.create('customer_logos_pull')
>>>结果= client.service.customer_logos_pull(customerLogosPull)

它在日志中给出了这个内容, 格式不正确(无效标记)

 (内容){ 
tag =auth
value =
{
token =xxx
}
type =<元素:0x106049290 name =auth >
< Complex:0x106049510>
<序列:0x106049590>
< Element:0x106049650 name =tokentype =(u'string',u'http://www.w3.org/2001/XMLSchema')/>
< Element:0x106049690 name =user_idtype =(u'string',u'http://www.w3.org/2001/XMLSchema')/>
<元素:0x1060496d0 name =user_tokentype =(u'string',u'http://www.w3.org/2001/XMLSchema')/>
< / Sequence>
< / Complex>
< / Element>
}

有人知道如何使用Python在标头中正确设置标记吗?所有提示都欢迎!

解决方案


$ b $ (显示与soap头对应的client.set_options(soapheaders = ssnp)

下面的soap请求xml

 < SOAP-ENV:Header> 
< xsi:SessionHeader>
< xsi:sessionId> xxxxxxx< / xsi:sessionId>
< / xsi:SessionHeader>
< / SOAP-ENV:标题>

我们可以使用 print client.last_sent()


I need to call a SOAP service with a message like this:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sub="https://secure.xpslogic.com/service/wijnen/sub">
   <soapenv:Header>
      <sub:auth>
         <token>?</token>
         <!--Optional:-->
         <user_id>?</user_id>
         <!--Optional:-->
         <user_token>?</user_token>
      </sub:auth>
   </soapenv:Header>
   <soapenv:Body>
      <sub:customer_logos_pull>
         <!--Optional:-->
         <language>?</language>
         <!--Optional:-->
         <limit>?</limit>
         <!--Optional:-->
         <options_utc>?</options_utc>
      </sub:customer_logos_pull>
   </soapenv:Body>
</soapenv:Envelope>

I've got some php example code, which sets the headers as follows (and which works perfectly well):

auth = array();$auth['token'] = 'xxx';if ($auth) { // add auth header $this->clients[$module]->__setSoapHeaders( new SoapHeader( $namespace, 'auth', $auth ) );}

I now construct the (empty) body and the header as follows with the Python suds lib:

from suds.client import Client
from suds import WebFault

client = Client(url='https://example.com/sub.wsdl')

auth = client.factory.create('auth')
auth.token = 'xxx'
client.set_options(soapheaders=auth)

customerLogosPull = client.factory.create('customer_logos_pull')
result = client.service.customer_logos_pull(customerLogosPull)

but this gives me a not well-formed (invalid token) message. When turning on logging I find this to be the message:

DEBUG:suds.mx.core:processing:
(Content){
   tag = "auth"
   value =
      (auth){
         token = "xxx"
         user_id = None
         user_token = None
      }
   type = <Element:0x10ff8c950 name="auth">
   <Complex:0x10ff8cbd0>
      <Sequence:0x10ff8cc50>
         <Element:0x10ff8cd10 name="token" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" />
         <Element:0x10ff8cd50 name="user_id" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" />
         <Element:0x10ff8cd90 name="user_token" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" />
      </Sequence>
   </Complex>
</Element>
 }

It looks pretty fine by me, but it also gives a not well-formed (invalid token). seeing that the suds docs has 3 examples on how to pass in soap headers, I tried the other two as well:

>>> token = client.factory.create('auth.token')
>>> token.set(TOKEN)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: token instance has no attribute 'set'

and

>>> client.set_options(soapheaders={'auth': {'token': 'xxx'}})
>>> customerLogosPull = client.factory.create('customer_logos_pull')
>>> result = client.service.customer_logos_pull(customerLogosPull)

which gives this content in the logs, and still a not well-formed (invalid token):

(Content){
   tag = "auth"
   value =
      {
         token = "xxx"
      }
   type = <Element:0x106049290 name="auth">
   <Complex:0x106049510>
      <Sequence:0x106049590>
         <Element:0x106049650 name="token" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" />
         <Element:0x106049690 name="user_id" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" />
         <Element:0x1060496d0 name="user_token" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" />
      </Sequence>
   </Complex>
</Element>
 }

Does anybody know how I can correctly set the token in the header using Python? All tips are welcome!

解决方案

I got my soap Headers working as below:

from suds.sax.element import Element
ssnp = Element("xsi:SessionHeader").append(Element('xsi:sessionId').setText("xxxxxxx"))
client.set_options(soapheaders=ssnp)

corresponding to soap Headers shown below in soap request xml

<SOAP-ENV:Header>
  <xsi:SessionHeader>
     <xsi:sessionId>xxxxxxx</xsi:sessionId>
  </xsi:SessionHeader>
</SOAP-ENV:Header>

We can see what request was sent using print client.last_sent()

这篇关于如何用Python suds库创建肥皂标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 19:22