Python Suds 凭证

0 投票
4 回答
3519 浏览
提问于 2025-04-17 08:14

我正在尝试在Python中使用SOAP API,但我似乎无法正确设置我的请求头。这里是相关的结构,你们有什么建议可以在suds中实现这个吗?

<xs:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://namespace.com">
  <xs:complexType name="Credentials"><xs:sequence/>
  <xs:attribute name="username" type="xs:string" use="required"/>
  <xs:attribute name="password" type="xs:string" use="required"/>
  <xs:attribute name="customerID" type="xs:int"/>
</xs:complexType>
<xs:element name="credentials" nillable="true" type="Credentials"/></xs:schema>

4 个回答

0

你在用哪个版本的suds呢?

import logging
logging.basicConfig(level=logging.INFO)
from suds.client import Client
url = 'wsdl url'
client = Client(url)
logging.getLogger('suds.client').setLevel(logging.DEBUG)
from suds.sax.element import Element
#create an xml element at our namespace
n = Element('credentials', ns=["cred","namespace.url"])
import suds.sax.attribute as attribute
#the username, customerid and pass are atributes so we create them and append them to the node. 
un = attribute.Attribute("username","your username")
up = attribute.Attribute("password","your password")
cid = attribute.Attribute("customerID",1111)
n.append(un).append(up).append(cid)
client.set_options(soapheaders=n)
0

因为你创建的这个元素是在你的WSDL文件中定义的,所以你可以通过客户端的工厂来创建它的实例:

n = client.factory.create('credentials')
n._username = "your username"
n._password = "your password"
n._customerID = 1111

client.set_options(soapheaders=n)

注意每个属性名称前面的_符号。这是为了区分那些和属性同名但不是属性的类型。

1

好的,我搞定了。看起来你可以设置自定义的xml节点,所以我们开始吧。

import logging
logging.basicConfig(level=logging.INFO)
from suds.client import Client
url = 'wsdl url'
client = Client(url)
logging.getLogger('suds.client').setLevel(logging.DEBUG)
from suds.sax.element import Element
#create an xml element at our namespace
n = Element('credentials', ns=["cred","namespace.url"])
import suds.sax.attribute as attribute
#the username, customerid and pass are atributes so we create them and append them to the node. 
un = attribute.Attribute("username","your username")
up = attribute.Attribute("password","your password")
cid = attribute.Attribute("customerID",1111)
n.append(un).append(up).append(cid)
client.set_options(soapheaders=n)

-CG

撰写回答