我试图用python和zeep发出SOAP请求,我需要添加一个soap:Header,带有security(wsse:Security)和timestamp(wsu:Timestamp)标签
我有这个代码:
from zeep import Client
from zeep.wsse import UsernameToken
from zeep.wsse import utils
from datetime import datetime, timedelta
# Create a SOAP client for the service
url = 'https://XXXXXXXXX/wsdlXXXXXX'
client = Client(url)
# Create a Object UsernameToken auth credentials
username = 'USERNAME'
password = 'PASSWORD'
token = UsernameToken(username, password)
# Add object UsernameToken to the tag wsse:Security
security = client.get_element('wsse:Security')
security_value = security(namelist=[token])
# Create a object Timestamp with date an time (now)
timestamp = utils.Timestamp()
timestamp_created = datetime.utcnow()
timestamp_expires = timestamp_created + timedelta(minutes=10)
timestamp.created = timestamp_created
timestamp.expires = timestamp_expires
# Add the tag wsu:Timestamp to the hader SOAP
timestamp_value = timestamp.xml
# add the tags in header SOAP
header = client.service._binding.create_message_header()
header.append(security_value)
header.append(timestamp_value)
# Send request SOAP with header
result = client.service.addressTown(_soapheaders=[header], arg1='28001')
我的错误是:
Traceback (most recent call last):
File "test3.py", line 16, in <module>
security = client.get_element('wsse:Security')
File "C:\Users\USER\Documents\Projects\Eva Seguros\comparatupoliza\venv\lib\site-packages\zeep\client.py", line 182, in get_element
return self.wsdl.types.get_element(name)
File "C:\Users\USER\Documents\Projects\Eva Seguros\comparatupoliza\venv\lib\site-packages\zeep\xsd\schema.py", line 126, in get_element
qname = self._create_qname(qname)
File "C:\Users\USER\Documents\Projects\Eva Seguros\comparatupoliza\venv\lib\site-packages\zeep\xsd\schema.py", line 265, in _create_qname
raise ValueError("No namespace defined for the prefix %r" % prefix)
ValueError: No namespace defined for the prefix 'wsse'
1条答案
按热度按时间zxlwwiss1#
wsse
前缀缺少一个名称空间,请在get_element方法中使用它们之前定义它。在下面的示例中,不要忘记将
XXXXXXXXX/wsdlXXXXXX
替换为WSDL的实际URL以及UsernameToken
对象的正确用户名和密码。