Zeep: Fast and Modern SOAP client for Python

Posted By :Mohit Bansal |27th June 2020

ZEEP

 

Zeep is the most performant and compliant Python XML library currently available. It can inspect the WSDL document of the server and create the corresponding code to use the services mentioned in the document. It avails an easy-to-use high-level interface to a SOAP server. 

 

 

                                                           

 

Zeep uses the XML library to parse the XML documents. It can process large SOAP responses at high speeds.

 

 

INSTALLATION

 

Zeep client can be installed using pip with the following command:

 

pip install zip

 

 

GETTING STARTED WITH ZEEP

 

We must have a WSDL file/URL to communicate with a soap server. Wsdl is a document having information about the server like server URL, available services and operations, etc. To inspect a WSDL file, use the following command: 

 

python -mzeep <wsdl>

 

 

Following is a sample code to create a client to communicate to a SOAP server using zeep:

 

 

import zeep

 

wsdl = './user/home/file.wsdl'

client = zeep.Client(wsdl=wsdl)

 

 

Most of the time, we may require to provide a username and password for HTTP Authentication while connecting to the server. Following code shows the authentication example:

 

 

import zeep

from requests import Session

from zeep.transports import Transport

 

 

wsdl = './user/home/file.wsdl'

session = Session()

session.auth = HTTPBasicAuth(<username>, <password>)

client = zeep.Client(wsdl=wsdl, transport=Transport(session=session))

 

 

THE SERVICE PROXY OBJECT

 

The purpose of the ServiceProxy object is to check if an operation exists for the attribute or item requested. If the operation does exist, the ServiceProxy object will return an OperationProxy object which is responsible for calling the operation on the binding. Following is an example to call an operation:

 

from zeep import Client

wsdl='http://my-endpoint.com/.svc?wsdl'

client = Client(wsdl=wsdl)

response = client.service.myOperation(arg1=val1, arg2=val2)

 

In the above snippet, a service is a ServiceProxy object. It will check if there is an operation with the name 'myOperation' defined in the binding and it will return an OperationProxy. In simple terms, it is like hitting an API on the server to get data in response. Zeep understands the response from the server and provides us in a simplified XML form.

 

To parse the xml data to find specific tags and conent one can use lxml library. Following snippet shows an example:

 

 

import lxml.etree as ET

root = ET.fromstring(<xml response>)

for child in root.getchildren():

   print(child.tag, child.text)

 

 

The above snippets provide the root of the XML. getchildren() provides the child tags of a root. tag attribute of a child gives the name of the tag and the text attribute gives the content of the tag.

 

 

For further details on the zeep library, please visit: https://docs.python-zeep.org/en/master/index.html

 
 
 
 
 
 

About Author

Mohit Bansal

He is tech enthusiast and always ready to learn new things. He has good skill in Python language.

Request For Proposal

[contact-form-7 404 "Not Found"]

Ready to innovate ? Let's get in touch

Chat With Us