我正在尝试使用python和zeep进行SOAP请求

kmpatx3s  于 2023-04-08  发布在  Python
关注(0)|答案(1)|浏览(161)

我试图用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'
zxlwwiss

zxlwwiss1#

wsse前缀缺少一个名称空间,请在get_element方法中使用它们之前定义它。
在下面的示例中,不要忘记将XXXXXXXXX/wsdlXXXXXX替换为WSDL的实际URL以及UsernameToken对象的正确用户名和密码。

from zeep import Client, Settings
from zeep.wsse import UsernameToken
from zeep.wsse.utils import Timestamp
from datetime import datetime, timedelta
from lxml import etree

# Create a SOAP client for the service
url = 'https://XXXXXXXXX/wsdlXXXXXX'
settings = Settings(strict=False, xml_huge_tree=True)
client = Client(url, settings=settings)

# Define the required namespaces
client.wsdl.types.prefix_map['wsse'] = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'
client.wsdl.types.prefix_map['wsu'] = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd'

# 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 = 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 header SOAP
timestamp_value = timestamp.xml

# Create SOAP header
header = etree.Element("SOAP-ENV:Header", nsmap=client.wsdl.types.prefix_map)
header.append(security_value)
header.append(timestamp_value)

# Send request SOAP with header
result = client.service.addressTown(_soapheaders=[header], arg1='28001')

相关问题