/ Python

SOAP with Python

SOAP with Python

Zeep library

Installation

yum install -y -q python-pip3 || sudo apt install -y python-pip3
sudo pip3 install -U pip zeep

Probe SOAP web service for endpoint

python3 -mzeep --no-verify https://10.222.1.151:8080/\?wsdl

Script example

#!/usr/bin/env python3
# coding=utf-8

from requests import Session
from zeep.transports import Transport
from zeep import Client
import hashlib, base64, struct, random
from zeep import xsd
from pprint import pprint
import datetime

# Debug
import logging.config
logging.config.dictConfig({
    'version': 1,
    'formatters': {
        'verbose': {
            'format': '%(name)s: %(message)s'
        }
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose',
        },
    },
    'loggers': {
        'zeep.transports': {
            'level': 'DEBUG',
            'propagate': True,
            'handlers': ['console'],
        },
    }
})

# No verify https
session = Session()
session.verify = False
transport = Transport(session=session)
url = 'https://10.222.1.151:8080'
login = 'adm'
password = hashlib.md5("admpassword".encode('utf-8')).hexdigest()

# Authentification
authentification = Client(url + '/login?wsdl',transport=transport)
user = authentification.service.login(login, password)

# SOAP Header
ns1 = '{http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd}';
ns2 = '{http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd}';

header = xsd.Element(
    ns1 + 'Security',
    xsd.ComplexType(
        ns1 + 'UsernameToken',
        [
            xsd.Attribute('{1}mustUnderstand'),
            xsd.ComplexType(
                [
                    xsd.Element( ns1 + 'Username', xsd.String()),
                    xsd.Element( ns1 + 'Password', xsd.String()),
                    xsd.Element( ns1 + 'Nonce', xsd.Base64Binary()),
                    xsd.Element( ns2 + 'Created', xsd.DateTime()),
                ]
            ),
        ]
    )
)

header_value = header(
    Username=user.login,
    Password=user.passwd,
    # Nonce=base64.b64encode(struct.pack('H', random.randint(1,10000))),
    Nonce=base64.b64encode(struct.pack('H', 12345)),
    Created=datetime.datetime.now(),
)

# Data
data = Client(url + '/soapserviceendpoint?wsdl',transport=transport)
data.set_default_soapheaders([header_value])

with data.options(raw_response=True):
    # probeMetrics = data.service.getProbeMetrics()
    # # pprint(vars(probeMetrics))   
SOAP with Python
Share this