selfuryon / netdev

Asynchronous multi-vendor library for interacting with network devices
http://netdev.readthedocs.io/
Apache License 2.0
213 stars 44 forks source link

Error handling - [Errno 111] Connect call failed ('10.10.10.2', 22) #12

Closed ajwillo closed 6 years ago

ajwillo commented 6 years ago

How do we handle errors when a device cannot be connected to? currently I et

File "/usr/local/lib/python3.6/asyncio/selector_events.py", line 480, in _sock_connect_cb raise OSError(err, 'Connect call failed %s' % (address,)) ConnectionRefusedError: [Errno 111] Connect call failed ('10.10.10.2', 22)

I would just like to print an exception i.e. couldn't connect to X. at the moment it aborts my script

I can see in base.py the timeout is set to 15 seconds, I tried to set the timeout in my params but got an error. how do we set timeout value?

Thanks

EDIT, resolve issue by upgrading net dev and having the following:

async def task(param):
    try:
        async with netdev.create(**param, timeout=5) as ios:
            # Testing sending simple command
            out = await ios.send_command("show ver")
            print(out)
    except:
        print('unable to connect to device')
selfuryon commented 6 years ago

Yes, it's right solution about using exception handling! One note about it - you may want to use asyncio.gather instead of asyncio.wait for awaiting coroutines due to wait doesn't catch the exception by default - you can read about it here