Example: Logging in and logging out using Python

The following example shows the parts of a Python program that handle the following:

  • Logging in

  • Logging out

  • Response codes

  • Disabling server certificate verification

  • Storing and passing the session cookie

# library to take inline args
import sys

# library to make REST requests
import requests

# regular expressions library
import re

# disable warnings that Web UI site is "insecure"
# needed to avoid connection timeout due to inability to verify server certificate
from requests.packages.urllib3 import disable_warnings
from requests.packages.urllib3.exceptions import InsecureRequestWarning

if len(sys.argv) is not 4:
    print("usage: 8400rest.py <switch_ip> <username> <password>")
    print("note: admin access is required to edit data")

# parameters
ver = 'v1'
sw_ip = sys.argv[1]
usr = sys.argv[2]
pswd = sys.argv[3]

def main():
    # class instance
    sw = sw_rest(ip=sw_ip, ver=ver)

    # only perform actions if login is successful
    if (sw.login(user=usr, passwd=pswd)):
        # Script action calls go here 

# class for rest calls to ArubaOS-CX switch
class sw_rest:

    def __init__(self, ip=None, ver=None):
        init class function

        :param str ip: ip address of switch to access (required)
        :param str ver: version of ArubaOS-CX REST (default 'v1')
        # check and set ip address
        if (ip is None):
            print("Switch IP address is required")
        else: self.ip = ip

        # check and set REST version
        if (ver is None):
            print("REST version not specified... using v1")
            self.ver = 'v1'
        else: self.ver = ver

        # create base address for REST requests (https://<ip>/rest/<ver>/)
        self.base_url = '/'.join(['https:/', self.ip])
        self.base_uri = '/'.join(['', 'rest', self.ver, ''])
        self.base = self.base_url + self.base_uri

        # cookie jar to store cookies after login
        self.cookie = requests.cookies.RequestsCookieJar()

    def login(self, user=None, passwd=None):
        login to switch

        :param str user: username for login (required)
        :param str passwd: password for login (required)
        :return: whether login was successful
        :rtype: bool
        self.login = {'username': user, 'password': passwd}
        # url = https://<ip>/rest/<ver>/login
        url = self.base + 'login'

        print("Logging in...")
        response = requests.post(

        # determine if login was successful
        status = self.status_handler(response.status_code)

        # set cookie if login is successful
        if (status):
            self.cookie.set('id', response.cookies['id'], domain=self.ip)

        return status

    def logout(self):
        logout of switch

        :return: whether logout was successful
        :rtype: bool
        # url = https://<ip>/rest/<ver>/logout
        url = self.base + 'logout'

        print("Logging out...")
        response = requests.post(

        return self.status_handler(response.status_code)

    def status_handler(self, status_code):
        determine if the response is good or bad

        :param int status_code: status code of response
        :return: whether response is good
        :rtype: bool
        return (status_code == requests.codes.ok)

if __name__ == "__main__":