Menu Close
    Convert your store into Multi Vendor Marketplace

    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:

    pip install zeep

    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:

    from zeep import Client
    wsdl = "https://wsvc.cdiscount.com/MarketplaceAPIService.svc?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 requests import Session
    from requests.auth import HTTPBasicAuth
    from zeep.transports import Transport
    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 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:

    request_data = {
        'account_id': '1234abc',
        'sender': {
            'street': '1226  Denver Avenue',
            'city': 'Anaheim',
            'state': 'CA',
            'zip': '92801',
            'country': 'US',
        },
        'receiver': {
            'street': '4040  Leo Street',
            'city': 'Pittsburgh',
            'state': 'PA',
            'zip': '15205',
            'country': 'US',
        },
        'attributes': {
            'item': {
                'id': '0',
                'unitPrice': '12.34',
                'quantity': '1',
            },
            'service': {
                'delivery': 'Ground',
            }
        }
    }
    

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

    <account_id>1234abc</account_id>
    <sender>
        <street>1226  Denver Avenue</street>
        <city>Anaheim</city>
        <state>CA</state>
        <zip>92801</zip>
        <country>US</country>
    </sender>
    <receiver>
        <street>4040  Leo Street</street>
        <city>Pittsburgh</city>
        <state>PA</state>
        <zip>15205</zip>
        <country>US</country>
    </receiver>
    <attributes>
        <item>
            <id>0</id>
            <unitPrice>12.34</unitPrice>
            <quantity>1</quantity>
        </item>
        <service>
            <delivery>Ground</delivery>
        </service>
    </attributes>

    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.sendData(**request_data)
    #Here 'request_data' is the request parameter dictionary.
    #Assuming that the operation named 'sendData' is defined in the passed wsdl.
    

    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:

    response['key']

    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!

    . . .
    Discuss on Helpdesk

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    15 comments

  • Jeroen Vermunt
    When I use the zeep client it returns an XML etree root object instead of a dictionary. Can I change it so zeep parses the xml instead a dictionary?

    the response is of type _value_1: ANY

    • Mohit Budakoti (Moderator)
      Hello Jeroen,
      With zeep library you can use zeep.helpers.serialize_object function to parse xml or AnyObject type to python dictionary. You need to pass response variable to this function.
      Regards
      Mohit Budakoti
  • far
    • Mohit Budakoti (Moderator)
      Hello 
      Did you tried to copy and paste this url into web browser window. Did you see xml file eg: https://prnt.sc/w4ise8, if not then its server problem or your network issue.
  • Antonio Samper
    Thanks for your contributions, I am on the topic of electronic invoicing, but I don’t know how to create an html based on the information contained in the wsdl, I don’t know how to add the information. I would appreciate your valuable help.
    • Anisha Bahukhandi (Moderator)
      Hello Antonio Samper,

      Thanks for your valuable feedback!
      Kindly share your requirements in detail; for that, you can drop us a mail at [email protected] or create a ticket.

      Regards
      Anisha Bahukhandi

  • Hamid
    Great, it is helpful
    Thank you for this sharing.
    • Anisha Bahukhandi (Moderator)
      Hello Hamid,
      Thanks for your valuable feedback. Stay tuned with our blogs.
      Regards
      Anisha Bahukhandi
  • 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 (Moderator)
      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 (Moderator)
      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 (Moderator)
      Hi Didier, please share the equivalent xml request format so that I can have a look. Thanks.
  • Back to Top