Menu Close
    Hire us Request quote Reading list Switch to dark mode

    Python SOAP Clients With Zeep

    Introduction:

    Working with SOAP based web services can sometimes be a time taking task when you have to write the complete XML for making API requests and then parse the response xml to fetch the desired results. That’s a headache right? Well, that’s when zeep comes into play. Zeep is a pure-python module. The best thing about Zeep is that you don’t have to write the XML at all. You just create a dictionary with all the relevant request data, and it will create the XML for you.

    Implementing zeep client:

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


    Now, in your python code, you’ll need to import Client from zeep and then instantiate the same by passing the wsdl url in it as shown below:



    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:



    Passing Parameters using Zeep:

    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.

    For instance:


    The above dictionary will be converted to the xml something similar to:


    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:

    The response object:

    Zeep response is similar to a python dictionary object(key-value pair) so it’s very easy to obtain the desired values from the response object using the following syntax:


    That’s it. If you like this post, it would be great if you could add your feedback and suggestions in the comment section. Also feel free to share your queries down below. Happy coding. 🙂

    Thanks!

    . . .

    Comments (7)

    Add Your Comment

  • Mukesh
    Thanks for simple and clear explanation about the zeep module. Could you please explain how to set the zeep xsd skipvalue property
    • Ravi Raushan
      Hi Mukesh,
      you can set the value like below code,

      client.service.submit_something(user_id=1, my_string=xsd.SkipValue)

      Regards,
      Ravi

  • Thomas
    Great, that helped me allot, was trying to fiquer this out for a long time.
    Now it’s easy. thanks
  • Eliakim
    Hi, I Enjoyed your post. You can add this line

    from zeep.transports import Transport

    If the person try to run with transport.

    Tks

    • Aditya Sharma
      Hi Eliakim,
      Thanks for the appreciation and sharing the info. The import statement has been added now 🙂
  • Didier
    Hi, thank you for sharing.
    I have an example of xml request that should go to the SOAP server like so:

    could you help me format it using dictionary with zeep?

    • Aditya Sharma
      Hi Didier, please share the equivalent xml request format so that I can have a look. Thanks.
  • Back to Top
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just received your message and our expert will get back to you shortly.
    Send Again
    Close