Suds Python Example

Problem this snippet solves:

This script will pull a list of virtual servers, then fetch any default pools associated with it. Edit your particular settings as needed (url, username, password, etc.)

How to use this snippet:

Script

Suds-sample.py

Code :

#!/bin/env python

# Small example of how to use the Python suds library with iControl.
# See comments for a description of each section.
# **Note: this requires suds version 0.3.6

# Use at your own discretion; for example purposes ONLY!

import sys
import logging
from urllib import pathname2url
from suds.client import Client


try:
     from suds.xsd.doctor import Import, ImportDoctor
except: 
     print "Requires suds 0.3.6 or higher!"
     sys.exit()

# Setup a log to see details on what is going on.
#logging.basicConfig(level=logging.INFO)
#logging.getLogger('suds.client').setLevel(logging.DEBUG)

# Create the URL. Note that you can point to the BigIP or the filesystem.
# On windows, something like this works:
# url = 'file:' + pathname2url('c:\\tmp') + '/LocalLB.VirtualServer.wsdl'

# Here, I'm grabbing it from a lab box and snagging the LocalLB.VirtualServer
# wsdl.

url = 'https://192.168.1.245/iControl/iControlPortal.cgi?WSDL=LocalLB.VirtualServer'

# Fix the missing import on our WSDLS.
imp = Import('http://schemas.xmlsoap.org/soap/encoding/')
doctor = ImportDoctor(imp)

# Create the 'client' object. Call against this object for your methods.
c = Client(url, doctor = doctor, username = 'admin', password = 'admin')

# Suds sets up a file cache that stores some common content, like your wsdl
# and any other basic GET style info. Uncomment to disable the cache.

#c.set_options(cache=None)

# Setup your endpoint, username, password, etc. against the object. Very
# pythonic! Do this by setting options attributes. 

c.options.username = 'admin'
c.options.password = 'admin'
c.options.location = 'https://192.168.1.245/iControl/iControlPortal.cgi'

# set the separator so we can create types. Python doesn't like using dot
# notations inside of the WSDLS, which are valid according to the spec. This accomodates dot
# notation. Special thanks to Jeff Ortel for adding this feature.
c.factory.separator('_')

# Call a basic method to get a list of pools on the box.

virtuals = c.service.get_list()

# Print the returned list. Your list is contained in the 'item' attribute of
# the returned object.

#for x in virtuals.item:
#    print x

# Now, we'll create a type and pass it in as a keyword argument to a call. It
# gets way more complex than this but we'll start slow...this part is a little painful, very java like ;).
# The point is that suds isvery explicit with its typing. Overall, this is a very good thing but it 
# makes for some challenges with more type-heavy calls (like Virtual Server
# create, for example).

# Create a type of 'StringSequence'. We'll set attributes here and pass this
# object into the keyword arguments. To create types, use the 'factory' object.
vs_name_list = c.factory.create('Common.StringSequence')
vs_name_list.item = virtuals.item

# Now let's call a method that allows us to pass in the type.

default_pools = c.service.get_default_pool_name(virtual_servers = vs_name_list)

# Now we'll print them after we zip() them together.

combined = zip(virtuals.item, default_pools.item)

for x in combined:
    print "%s => %s" % (x[0], x[1])
Published Mar 09, 2015
Version 1.0

Was this article helpful?

1 Comment

  • python f5_1.py Traceback (most recent call last): File "f5_1.py", line 77, in vs_name_list.item = virtuals.item AttributeError: 'list' object has no attribute 'item'