29.04.2019 Views

OS6860(E)_AOS_8.1.1.R01_Switch_Management_Guide

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Web Services, CLI Scripting, and OpenFlow<br />

Using Python<br />

Using Python<br />

Python is an easy to learn, powerful, general-purpose scripting language. It combines easily readable code<br />

with an object-oriented programming approach for fast and easy development on many platforms. Additional<br />

information on Python as well as installation instructions can be found from the Python website:<br />

http://www.python.org.<br />

A Python library has been created which can be used by any Python Consumer communicating with the<br />

<strong>AOS</strong> Web Service Provider. The library is available in source form and provides a tool allowing developers<br />

to learn how to write code that communicates with the Omni<strong>Switch</strong> Web Service Provider. In addition,<br />

this library can also be used as a standalone query tool using the command line.<br />

Library Use<br />

Invoking the library from third-party code is as simple as importing the relevant classes:<br />

from consumer import <strong>AOS</strong>API, <strong>AOS</strong>Connection<br />

The library itself relies on the dependency injection pattern, allowing the implementer to replace only bits<br />

of the library with their own code, should they need to do so. The two example components imported<br />

above allow a connection to be established to an <strong>AOS</strong> device.<br />

Connection Example<br />

A typical connection to an <strong>AOS</strong> device should look like this:<br />

def do_something():<br />

try:<br />

api = <strong>AOS</strong>API(<strong>AOS</strong>Connection(<br />

username = 'admin',<br />

password = 'switch',<br />

hostaddress = '192.168.1.1',<br />

secure = False,<br />

obeyproxy = False,<br />

prettylinks = True,<br />

useport = 80,<br />

aosheaders = None,<br />

debug = True))<br />

api.login()<br />

# Additional code goes here<br />

api.logout()<br />

except HTTPError, e:<br />

api.logout()<br />

print "Error: " + e.msg<br />

Query Example<br />

Augmenting the code above to perform a query is straightforward. Simply call api.query() and check its<br />

success() property as in the example below:<br />

results = api.query('mib', 'chasEntTemperatureTable', {<br />

'mibObject0':'chasEntTempCurrent',<br />

'mibObject1':'chasEntTempThreshold',<br />

Omni<strong>Switch</strong> <strong>AOS</strong> Release 8 <strong>Switch</strong> <strong>Management</strong> <strong>Guide</strong> May 2014 page 10-15

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!