Working with SOAP based Web Service using Python

AYUSHI SHARMA
3 min readMay 24, 2019

--

Web services is a standardized way or medium to propagate communication between the client and server applications on the World Wide Web. Web services provide a common platform that allows multiple applications built on various programming languages to have the ability to communicate with each other

What is SOAP?

SOAP is known as the Simple Object Access Protocol.

SOAP is simply an XML-based messaging protocol for exchanging information among computers, , as explained in detail here. But hey, who would like to write lines of XML code for only a simple request? Yeah, neither would I. But, the good thing is that we actually need not to. Because, Zeep does all the “hard work” for us while all we need to do is to create a dictionary that contains the required fields.

What is ZEEP ?

Zeep is a pure-python module. Zeep inspects the WSDL document and generates the corresponding code to use the services and types in the document. This provides an easy to use programmatic interface to a SOAP server.

You can read the documentation for ZEEP here.

Implementing ZEEP Client:

The first thing that you’ll need to do is, install the zeep python library as:

pip install zeep

In my case, the client’s system had a relatively simple XML schema for requests. Pretty simple, I admit.

<username>SAP_username</username>
<password>SAP_password</password>

Now, in your python code, you’ll need to import Client from zeep and then instantiate the same by passing the WSDL URL. This can simply be done as follows:

from zeep import Client
wsdl = "http://xxxxxxxxxxx:8181/ws/xxxxxxxxx?wsdl"
client = Client(wsdl)

You can also pass user autherntication details(username and passowrd) in case the wsdl is password protected. For this, you’ll need to create the Session object as shown below:

from zeep import Client
from zeep.transports import Transport
from requests import Session
from requests.auth import HTTPBasicAuth
wsdl = <wsdl_url>
session = Session()
session.auth = HTTPBasicAuth(<username>, <password>)

#An additional argument ‘transport’ is passed with the authentication details

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

Passing Parameters

As I mentioned above, you just need to pass the request parameters as a standard python dictionary and zeep will internally convert it to the final xml.

request_data={'SAPUsername' : SAP_username ,
'SAPPassword' : SAP_password}

Zeep detects the xml schema from the wsdl file that is passed while creating the client object. Wsdl(Web Service Description Language) contains the required description about the endpoint. It contains the list of operations available and also states the parameters that each operation requires. Zeep uses this information to map the passed request dictionary to the corresponding xml.

Envoking Web Service Methods:

You can use the operations defined in the wsdl using the simple syntax shown below:

response=client.service.xxxxxxx(**request_data)#Here 'request_data' is the request parameter dictionary.

It’s worth mentioning that I had my operation, i.e. “xxxxxxx” already defined in the client’s XML Schema as<wsdl operation name=“xxxxxxx”> So, it can return a valid response once it receives the required arguments, e.g. SAP_username, SAP_password.

Last Words

I hope this article helps you with making your first SOAP requests in Python and saves you some time from spending hours scrolling through pages of documentation for a very simple task. Here you can have a look at my script on GitHub Gist if you’d like to see the complete code.

#Assuming that the operation named 'sendData' is defined in the passed wsdl.

--

--

AYUSHI SHARMA

Data Analyst || Tableau Developer || Aspiring Data Scientist