How can I obtain a SIP peer list using pyst?

802 Views Asked by At

I would like to show Asterisk SIP peers in my Python application.

I can see such list executing:

[pbx:~] # asterisk -rx "sip show peers"
Name/username    Host              Dyn Forcerport Comedia    ACL Port     Status      Description
212              (Unspecified)      D  No         No             0        UNKNOWN
213              (Unspecified)      D  No         No             0        UNKNOWN
217/217          169.0.5.17         D  No         No             5060     OK (29 ms)
....

In Python I use pyst 0.8 library:

import asterisk.manager
...
manager = asterisk.manager.Manager()
try:
    manager.connect(host, port)
    manager.login(usr, passwd)
    r = manager.sippeers()

    print('------- r ----')
    pp.pprint(r)
    print('-----------')
    # ...

Then I pretty print some fields of the response. But all I get is:

------- r ----
Success
-----------
------ response -----
[   u'Response: Success\r\n',
    u'ActionID: pbx-25332-00000001\r\n',
    u'EventList: start\r\n',
    u'Message: Peer status list will follow\r\n']
-----------
------ data -----
u''
-----------
------ headers -----
{   u'ActionID': u'pbx-25332-00000001',
    u'EventList': u'start',
    u'Message': u'Peer status list will follow',
    u'Response': u'Success'}
-----------
----- multiheaders ------
{   u'ActionID': [u'pbx-25332-00000001'],
    u'EventList': [u'start'],
    u'Message': [u'Peer status list will follow'],
    u'Response': [u'Success']}
-----------

How can I obtain list of SIP peers using pyst?

Asterisk version:

asterisk -r -vvvvv
Asterisk 11.17.1, Copyright (C) 1999 - 2013 Digium, Inc. and others. ...
1

There are 1 best solutions below

2
miken32 On BEST ANSWER

sippeers() is just a shortcut for sending the SIPpeers command directly. It doesn't do any listening for resulting events. According to documentation you get Peerlist and PeerlistComplete events after sending the command, so you will need to register a listener for the events. I haven't worked with pyst, so I'm not familiar with the structure of the events, but hopefully this helps.

import asterisk.manager
...

def evt_handler(evt, mgr):
    print(f'Got event {evt.name}')
    # do something with evt.data or evt.message

    if evt.name == 'PeerlistComplete':
        mgr.close()
    
manager = manager.Manager()
try:
    manager.connect(host, port)
    manager.login(usr, passwd)
    manager.register_event('Peerlist', evt_handler)
    manager.register_event('PeerlistComplete', evt_handler)
    manager.sippeers()