[英]Python Suds credentials
我正在嘗試在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>
好吧,我知道了。 看來您可以設置自定義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
您正在使用哪個版本的肥皂水?
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)
由於您要創建的元素是在WSDL中定義的,因此可以使用客戶端的工廠創建該元素的實例:
n = client.factory.create('credentials')
n._username = "your username"
n._password = "your password"
n._customerID = 1111
client.set_options(soapheaders=n)
注意每個屬性名稱前的_
。 這將它們與具有相同名稱的類型中的非屬性區分開來。
我只想分享我可以使用我的憑據進行連接的方式,希望對您有所幫助:
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.