View Only

Async NETCONF RPC example with ncclient

By mwiget posted 06-02-2017 08:15


Async NETCONF RPC example with ncclient

ncclient is a great Python library that facilitates client-side scripting and application development around the NETCONF protocol: and used by the even more popular PyEz (


Typically, one uses the provided functions in synchronous mode, where replies are awaited and the result returned. I was however in need of asynchronous mode, where operations return immediately, allowing me to do ‘other stuff’ and periodically check if a result came in and process it accordingly.

In search of example code using async mode turned out to be harder than I thought. Starting from a synchronous example script that sends a RPC command to retrieve software information and extract the hostname from it (, I ended up with the following working asynchronous version of the same script:


from ncclient import manager
from ncclient.xml_ import *
import time
from ncclient.devices.junos import JunosDeviceHandler

def connect(host, port, user, password):
    conn = manager.connect(host=host,
                           device_params={'name': 'junos'},

    junos_dev_handler = JunosDeviceHandler(
        device_params={'name': 'junos',
                       'local': False})

    conn.async_mode = True

    rpc = new_ele('get-software-information')
    obj = conn.rpc(rpc)

    # for demo purposes, we just wait for the result
    while not obj.event.is_set():
        print('waiting for answer ...')

    result = NCElement(obj.reply,

    print 'Hostname: ', result.findtext('.//host-name')

if __name__ == '__main__':
    connect('pe1', 830, 'lab', 'lab123')


Executed against a device:


$ python
waiting for answer ...
waiting for answer ...
Hostname:  pe1


1 comment
1 view



06-11-2018 14:53

I don't think ncclient support async really. We can set conn to async_mode, which make dispatch async. But manager.connect is not async and it take 1~2 seconds, make async meaningless when you operate many devices.

Is there a attempt to add asyncio to ncclient?


Liang Cao