Thought leadership from the most innovative tech companies, all in one place.

Work with WSDL Files Using Python and Zeep

The Web Services Description Language (WSDL) is an XML-based interface description language that is used for describing the functionality offered by a web service. WSDL description of a web service (also referred to as a WSDL file) provides a machine-readable description of how the service can be called, what operations it will perform, what parameters it expects, and what data structures it returns.

image

Zeep Python module

To work with WSDL files using Python programming language, one can use Zeep, a pure-python module. Zeep is a fast and modern SOAP client for Python. It is compatible with Python versions 3.6, 3.7, 3.8 and PyPy, and has support for SOAP 1.1, SOAP 1.2 and HTTP bindings. Zeep inspects the WSDL file and generates the corresponding code to use the services and types in the document. Zeep has lxml dependency as it uses libxml2 and libxslt libraries.

Below are the steps to work with WSDL files using Python and Zeep:

Step 1. Install Zeep and lxml 4.2.5 with pip package installer for Python

pip install lxml==4.2.5 zeep

Note: pip is already installed if you are using Python 2 >=2.7.9 or Python 3 >=3.4 downloaded from python.org or if you are working in a Virtual Environment created by virtualenv or venv.

Zeep 4.0 is the the latest version to support Python 2.7 and Python 3.5

pip install zeep==4.0.0

Step 2. Write the Python code to connect to the WSDL file

from zeep import Client

wsdl = 'http://www.soapclient.com/xml/soapresponder.wsdl'
client = Client(wsdl=wsdl)
client.service.Method1('Zeep', 'is cool')

The WSDL file (SoapResponder.wsdl) used here is a sample from SOAPClient website.

The WSDL used here defines one simple function (Method1) which is made available by Zeep via client.service.Method1. It takes two arguments and returns a string (below).

image

Method1 specifications in SoapResponder.wsdl

Some WSDL files/documents need Basic access authentication while making a request. For such files, one can use the below Python code:

from zeep import Client
from requests import Session
from requests.auth import HTTPBasicAuth
from zeep.transports import Transport
from zeep.exceptions import Fault
try:
  wsdl = <WSDL File or WSDL URL>
  session = Session()
  session.auth = HTTPBasicAuth(username, password)
  client = Client(wsdl=wsdl, transport=Transport(session=session))
except Fault as fault:
  parsed_fault_detail = client.wsdl.types.deserialize(fault.detail[0])

In some situations, one needs to change the SOAP end-point address from the one which is defined within the WSDL or the WSDL doesn't define any service elements. The same can be achieved by creating a new ServiceProxy using the Client.create_service()method.

Below piece of Python code changes the end-point URL and creates a new ServiceProxy. The same function can be used to append any parameter to the existing SOAP address.

new_service = get_service(client=client, translation=(old_address, new_address))def get_service(client, translation):
  if translation:
    service_binding = client.service._binding.name
    service_address = client.service._binding_options['address']
    return client.create_service(
      service_binding,
      service_address.replace(*translation, 1))
  else:
    return client.service

More about Zeep can be found in their official documentation.

Hope this helps. Happy coding!




Continue Learning