Manager

This module provides a Python API for interfacing with the asterisk manager.

Example

import asterisk.manager
import sys

def handle_shutdown(event, manager):
   print "Recieved shutdown event"
   manager.close()
   # we could analize the event and reconnect here

def handle_event(event, manager):
   print "Recieved event: %s" % event.name

manager = asterisk.manager.Manager()
try:
    # connect to the manager
    try:
       manager.connect('host')
       manager.login('user', 'secret')

        # register some callbacks
        manager.register_event('Shutdown', handle_shutdown) # shutdown
        manager.register_event('*', handle_event)           # catch all

        # get a status report
        response = manager.status()

        manager.logoff()
    except asterisk.manager.ManagerSocketException as e:
       print "Error connecting to the manager: %s" % e.strerror
       sys.exit(1)
    except asterisk.manager.ManagerAuthException as e:
       print "Error logging in to the manager: %s" % e.strerror
       sys.exit(1)
    except asterisk.manager.ManagerException as e:
       print "Error: %s" % e.strerror
       sys.exit(1)

finally:
   # remember to clean up
   manager.close()

Remember all header, response, and event names are case sensitive.

Not all manager actions are implmented as of yet, feel free to add them and submit patches.

Specification

class asterisk.manager.Event(message)[source]

Manager interface Events, __init__ expects and ‘Event’ message

get_header(hname, defval=None)[source]

Return the specfied header

has_header(hname)[source]

Check for a header

class asterisk.manager.ManagerMsg(response)[source]

A manager interface message

get_header(hname, defval=None)[source]

Return the specfied header

has_header(hname)[source]

Check for a header

parse(response)[source]

Parse a manager message