This article explains how to use Traffic Manager's REST Control API using the excellent requests Python library.
There are many ways to install the requests library. On my test client (MacOSX), the following was sufficient:
$ sudo easy_install pip
$ sudo pip install requests
The REST API gives you access to the Traffic Manager Configuration, presented in the form of resources. The format of the data exchanged using the RESTful API will depend on the type of resource being accessed:
Working with JSON and Python
The json module provides functions for JSON serializing and deserializing. To take a Python data structure and serialize it into JSON format use json.dumps() and to deserialize a JSON formatted string into a Python data structure use json.loads().
Working with a RESTful API and Python
To make the programming easier, the program examples that follow utilize the requests library as the REST client. To use the requests library you first setup a requests session as follows, replacing <userid> and <password> with the appropriate values:
client = requests.Session() client.auth = ('<userid>', '<password>') client.verify = False
The last line prevents it from verifying that the certificate used by Traffic Manager is from a certificate authority so that the self-signed certificate used by Traffic Manager will be allowed. Once the session is setup, you can make GET, PUT and DELETE calls as follows:
response = client.get() response = client.put(, data = , headers = ) response = client.delete()
The URL for the RESTful API will be of the form:
<STM hostname or IP>:9070/api/tm/1.0/config/active/
followed by a resource type or a resource type and resource, so for example to get a list of all the pools from the Traffic Manager instance, stingray.example.com, it would be:
And to get the configuration information for the pool, “testpool” the URL would be:
For most Python environments, it will probably be necessary to install the requests library. For some Python environments it may also be necessary to install the httplib2 module.
JSON responses from a GET or PUT are deserialized into a Python dictionary that always contains one element. The key to this element will be:
Please see Feature Brief: Traffic Manager's RESTful Control API for examples of these data structures and something like the Chrome REST Console can be used to see what the actual data looks like.