Python Suds 凭证

发布于 2024-12-20 20:44:53 字数 603 浏览 4 评论 0原文

我正在尝试在 python 中使用soap api,但是我似乎无法正确设置我的标头。这是架构,有什么想法如何在泡沫中实现这一点吗?

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

I am attempting to work with soap api in python however I cannot seem to get my headers properly set. Here is the schema, any ideas how to accomplish this in 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

枯叶蝶 2024-12-27 20:44:53

好的,我成功了。看来你可以设置自定义 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

Ok, I got it working. It seems you can set custom xml nodes so here we go

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

日记撕了你也走了 2024-12-27 20:44:53

由于您要创建的元素是在 WSDL 中定义的,因此您可以使用客户端的工厂创建它的实例:

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

client.set_options(soapheaders=n)

请注意每个属性名称之前的 _。这将它们与类型中具有相同名称的非属性区分开来。

Since the element you're creating is defined in your WSDL, you can create an instance of it using the client's factory:

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

client.set_options(soapheaders=n)

Note the _ before each of the attribute names. This distinguishes them from non-attributes in the type that have the same name.

绾颜 2024-12-27 20:44:53

您使用的是哪个版本的肥皂水?

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)

which version of suds are you using?

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)
泅渡 2024-12-27 20:44:53

我只是想分享我可以连接我的凭据的方式,希望对您有所帮助:

from suds.client import Client
from suds.wsse import *
import suds.bindings

WSDL_URL = 'https://...?wsdl'
URL =  'https://...'
WSSE_USERNAME = 'wsse_username'
WSSE_PASSWORD = 'wsse_password'
USUARIO = 'my_user'
PASSWORD = 'my_password'

suds.bindings.binding.envns = ('SOAP-ENV', 'http://www.w3.org/2003/05/soap-envelope')
client = Client(WSDL_URL,cache=None)
security = Security()
token = UsernameToken(WSSE_USERNAME, WSSE_PASSWORD)
security.tokens.append(token)
client.set_options(wsse=security)
client.set_options(location=URL)
arrayMedicamentosDTO = []

medicamentosDTO = client.factory.create('medicamentosDTO')
medicamentosDTO.f_evento = '14-03-2015'
arrayMedicamentosDTO.append(medicamentosDTO) 

response = client.service.sendMedicamentos(arrayMedicamentosDTO, USUARIO, PASSWORD)

I just want to share the way i could connect with my credentials, i hope it help you:

from suds.client import Client
from suds.wsse import *
import suds.bindings

WSDL_URL = 'https://...?wsdl'
URL =  'https://...'
WSSE_USERNAME = 'wsse_username'
WSSE_PASSWORD = 'wsse_password'
USUARIO = 'my_user'
PASSWORD = 'my_password'

suds.bindings.binding.envns = ('SOAP-ENV', 'http://www.w3.org/2003/05/soap-envelope')
client = Client(WSDL_URL,cache=None)
security = Security()
token = UsernameToken(WSSE_USERNAME, WSSE_PASSWORD)
security.tokens.append(token)
client.set_options(wsse=security)
client.set_options(location=URL)
arrayMedicamentosDTO = []

medicamentosDTO = client.factory.create('medicamentosDTO')
medicamentosDTO.f_evento = '14-03-2015'
arrayMedicamentosDTO.append(medicamentosDTO) 

response = client.service.sendMedicamentos(arrayMedicamentosDTO, USUARIO, PASSWORD)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文