Python SOAP client with Suds

In this article i am going to explain briefly the implementation of Suds Client. Suds is a lightweight  library that uses SOAP based clients for python. SOAP is an RPC (Remote Procedure Call) that uses object-oriented protocol. Suds is actually lightweight  SOAP python client that provides a service proxy  for web services.

Implementing suds Client

For implementing the suds client follow the below steps.

  • Install suds library. Suds can be installed as,
    sudo pip install suds   or  sudo apt-get install python-suds
  • Import Client from suds as shown in the example below.
  • Instantiate the suds client by providing the WSDL path. The WSDL path can be a WSDL URL or WSDL  file(with .wsdl extension, local or remote). In the below example i have given a WSDL URL and i will explain how to create a client using this WSDL URL.
    from suds.client import Client
    client = Client("")
  • Now the suds client is ready to use. The suds client contains the operations present in the WSDL as methods and all the data types as a factory of Types. If you want to see what actually the client contains you can console it. In the below image i have shown the output of above created client.
    from suds.client import Client
    client = Client("")
    print client
    # Content inside the client
    Suds ( )  version: 0.7.dev0
    Service ( MarketplaceAPIService ) tns=""
       Prefixes (7)
          ns0 = ""
          ns1 = ""
       Ports (1):
             Methods (30):
                GetAllowedCategoryTree(ns0:HeaderMessage headerMessage)
                GetBrandList(ns0:HeaderMessage headerMessage)
                GetDiscussionMailList(ns0:HeaderMessage headerMessage, ns6:GetDiscussionMailListRequest request)
                GetSellerIndicators(ns0:HeaderMessage headerMessage)
                GetSellerInformation(ns0:HeaderMessage headerMessage)
                ManageParcel(ns0:HeaderMessage headerMessage, ns6:ManageParcelRequest manageParcelRequest)
                SubmitOfferPackage(ns0:HeaderMessage headerMessage, ns6:OfferPackageRequest offerPackageRequest)
             Types (193):
  • In this article i will consider only “GetSellerInformation”. For calling a method in suds we need to create a factory and i will explain how to create the  factory for this method.  GetSellerInformation takes “HeaderMessage” as an argument and we need to create first a factory for “HeaderMessage”. Before creating the factory of the “HeaderMessage”  we should be aware of how we have to create it.  In order to  see the architecture of it we can console it. Let us print the  how actually “HeaderMessage” factory looks like and how we need to create it.
    from suds.client import Client
    client = Client("")
    HeaderMessage = client.factory.create('ns0:HeaderMessage')
    print HeaderMessage
       Context = 
             CatalogID = None
             CustomerID = None
             CustomerId = None
       Localization = 
             Country = 
                   value = None
             Currency = 
                   value = None
             Language = 
                   value = None
       Security = 
             DomainRightsList = 
                   DomainRights[] = <empty>
             IssuerID = None
             SessionID = None
             TokenId = None
             UserName = None
       Version = None
  • This is how the “HeaderMessage” actually looks like and we have to create the factory for a method accordingly based on this format.  Once the factory for “HeaderMessage” is ready we can call the “GetSellerInformation” method and pass the HeaderMessage in it as a paramater. I have explained in the below example how we can create the suds factory for “HeaderMessage”.
    from suds.client import Client
    client = Client("")
    HeaderMessage = client.factory.create('ns0:HeaderMessage')
    #   Create a factory and assign the values
    HeaderMessage.Context =  {
         "CatalogID" = XXXXX
         "CustomerID" = XXXXX
         "SiteID" = XXXXX
    HeaderMessage.Localization =  {
        "Country": "Fr",
        "Currency": "Eur",
        "DecimalPosition": XXXXX,
        "Language": "French"
    SecurityContext = client.factory.create('ns0:SecurityContext')
    ArrayOfDomainRights = client.factory.create('ns0:ArrayOfDomainRights')
    ArrayOfDomainRights.DomainRights = [XXXXXXXXXXXXX, XXXXXXXXXXXX]
    SecurityContext.DomainRightsList = ArrayOfDomainRights
    SecurityContext.IssuerID = XXXXXXXX
    SecurityContext.UserName = XXXXXXXXX
    SecurityContext.TokenId = XXXXXXXXXXXXXXXXXXXXXX
    HeaderMessage.Security = SecurityContext
    HeaderMessage.Version = '1.0'
    # call the GetSellerInformation and pass the HeaderMessage
    response = client.service.GetSellerInformation(HeaderMessage)
  • The response from a suds call  is in the object format, we can access the content inside the response directly using the dot(.) This is an example of the suds response.
    print response
    # Suds response of GetSellerInformation
       ErrorMessage = None
       OperationSuccess = True
       ErrorList = None
       SellerLogin = "XXXXXXXXXXXX"
       Seller = 
             Email = "XXXXXXXXXXXXXXXXXX"
             IsAvailable = "ActiveSeller"
             Login = "XXXXXXXXXXXXXX"
             MobileNumber = "XXXXXXXXXXXXXXX"
             PhoneNumber = "XXXXXXXXXXXXX"
             SellerAddress = 
                   Address1 = "XXXXXXXXXXXX"
                   Address2 = "XXXXXXXXXXXX"
                   ApartmentNumber = None
                   Building = None
                   City = "XXXXXXXXXXX"
                   FirstName = None
                   ZipCode = "XXXXXXXXXXXX"
             ShopName = "XXXXXXXXXXXXXXX"
             ShopUrl = None
             SiretNumber = "XXXXXXXXXXXXX"
             State = "Activated"
  • If you want to convert the response into the serializable json format you need to add the following code to convert it into the serializable json format.
    import json
    from suds.sudsobject import asdict
    def recursive_dict(d):
        out = {}
        for k, v in asdict(d).iteritems():
            if hasattr(v, '__keylist__'):
                out[k] = recursive_dict(v)
            elif isinstance(v, list):
                out[k] = []
                for item in v:
                    if hasattr(item, '__keylist__'):
                out[k] = v
        return out
    response =  json.dumps(recursive_dict(response))
  • After the suds response is passed through this method it converts it into the  serilizable json format as shown below.
     {"SellerLogin": "webkultest",
    "OperationSuccess": true, 
    "ErrorMessage": null,
    "ErrorList": null, 
    "Seller": {
    "ShopUrl": null, 
    "MobileNumber": "XXXXXXXXXXXXX", 
    "ShopName": "XXXXXXXXX", 
    "IsAvailable": "ActiveSeller",
    "State": "Activated", 
    "PhoneNumber": "XXXXXXXXXXXX", 
    "Login": "webkultest", 
    "Email": "XXXXXXXXXXXXXX", 
    "ZipCode": "XXXXXXXXXXX"}

Some useful methods used in suds

  • client.last_sent() # displays the latest the request of the suds call
    client.last_received() #displays the latest response of the suds call.
  • For full debugging the output use the following methods.
    import logging

That is it.!!!
If you liked this post, It would be very grateful if you write your opinions, comments and suggestions to keep the post updated and interesting.
Thank you!

Current Product Version - 1.0

Supported Framework Version - python

. . .

Comments (2)

Add Your Comment

  • chetan
    response = client.service.QuoteDetailRequest(Header, Request)
    print response
    I got error on above, how can I eliminates.
    AttributeError: ‘NoneType’ object has no attribute ‘name’
  • chetan
    When I call web services then got error. plz, tell me how it resolve.

    result = self.failed(binding, e)
    File “/usr/lib/python2.7/dist-packages/suds/”, line 714, in failed
    raise Exception((status, reason))
    Exception: (404, u’Not Found’)

  • css.php