How can I consume a WSDL (SOAP) web service in Python? How can I consume a WSDL (SOAP) web service in Python? python python

How can I consume a WSDL (SOAP) web service in Python?


I would recommend that you have a look at SUDS

"Suds is a lightweight SOAP python client for consuming Web Services."


There is a relatively new library which is very promising and albeit still poorly documented, seems very clean and pythonic: python zeep.

See also this answer for an example.


I recently stumbled up on the same problem. Here is the synopsis of my solution:

Basic constituent code blocks needed

The following are the required basic code blocks of your client application

  1. Session request section: request a session with the provider
  2. Session authentication section: provide credentials to the provider
  3. Client section: create the Client
  4. Security Header section: add the WS-Security Header to the Client
  5. Consumption section: consume available operations (or methods) as needed

What modules do you need?

Many suggested to use Python modules such as urllib2 ; however, none of the modules work-at least for this particular project.

So, here is the list of the modules you need to get. First of all, you need to download and install the latest version of suds from the following link:

pypi.python.org/pypi/suds-jurko/0.4.1.jurko.2

Additionally, you need to download and install requests and suds_requests modules from the following links respectively ( disclaimer: I am new to post in here, so I can't post more than one link for now).

pypi.python.org/pypi/requests

pypi.python.org/pypi/suds_requests/0.1

Once you successfully download and install these modules, you are good to go.

The code

Following the steps outlined earlier, the code looks like the following:Imports:

import loggingfrom suds.client import Clientfrom suds.wsse import *from datetime import timedelta,date,datetime,tzinfoimport requestsfrom requests.auth import HTTPBasicAuthimport suds_requests

Session request and authentication:

username=input('Username:')password=input('password:')session = requests.session()session.auth=(username, password)

Create the Client:

client = Client(WSDL_URL, faults=False, cachingpolicy=1, location=WSDL_URL, transport=suds_requests.RequestsTransport(session))

Add WS-Security Header:

...addSecurityHeader(client,username,password)....def addSecurityHeader(client,username,password):    security=Security()    userNameToken=UsernameToken(username,password)    timeStampToken=Timestamp(validity=600)    security.tokens.append(userNameToken)    security.tokens.append(timeStampToken)    client.set_options(wsse=security)

Please note that this method creates the security header depicted in Fig.1. So, your implementation may vary depending on the correct security header format provided by the owner of the service you are consuming.

Consume the relevant method (or operation) :

result=client.service.methodName(Inputs)

Logging:

One of the best practices in such implementations as this one is logging to see how the communication is executed. In case there is some issue, it makes debugging easy. The following code does basic logging. However, you can log many aspects of the communication in addition to the ones depicted in the code.

logging.basicConfig(level=logging.INFO) logging.getLogger('suds.client').setLevel(logging.DEBUG) logging.getLogger('suds.transport').setLevel(logging.DEBUG)

Result:

Here is the result in my case. Note that the server returned HTTP 200. This is the standard success code for HTTP request-response.

(200, (collectionNodeLmp){   timestamp = 2014-12-03 00:00:00-05:00   nodeLmp[] =       (nodeLmp){         pnodeId = 35010357         name = "YADKIN"         mccValue = -0.19         mlcValue = -0.13         price = 36.46         type = "500 KV"         timestamp = 2014-12-03 01:00:00-05:00         errorCodeId = 0      },      (nodeLmp){         pnodeId = 33138769         name = "ZION 1"         mccValue = -0.18         mlcValue = -1.86         price = 34.75         type = "Aggregate"         timestamp = 2014-12-03 01:00:00-05:00         errorCodeId = 0      }, })