简体   繁体   English

Python Suds凭证

[英]Python Suds credentials

I am attempting to work with soap api in python however I cannot seem to get my headers properly set. 我正在尝试在python中使用soap api,但是似乎无法正确设置标头。 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>

Ok, I got it working. 好吧,我知道了。 It seems you can set custom xml nodes so here we go 看来您可以设置自定义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 -CG

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)

Since the element you're creating is defined in your WSDL, you can create an instance of it using the client's factory: 由于您要创建的元素是在WSDL中定义的,因此可以使用客户端的工厂创建该元素的实例:

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. 这将它们与具有相同名称的类型中的非属性区分开来。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM