Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
asyncio question
#1
Hello
It is my first post.
I would like to use asyncio for a network module. The purpose is to send the same request to multiple devices and waiting for the answer from all.

I tried the following code but it is not working.
import asyncio
from datetime import datetime
from jsonrpclib import Server
import os, sys, re
import time
import json
from netaddr import *


# username and password for the connection
username = ''
password = ''

deviceNotConnected=[]

devices =['spine1','spine2','leaf1-A','leaf1-B','leaf2-A','leaf2-B','leaf3-A','leaf4']
commandes=['show lldp neighbors detail',
            'show version',
            'show interfaces status',
            'show ip interface',
            'show ip bgp summary']

# Connection to the switch
async def openconnexion(device,username,password,cde):
  switch = Server("http://%s:%s@%s/command-api" %(username,password,device))
  try:
    result = switch.runCmds(version = 1, cmds = cde)
  except:
    deviceNotConnected.append(device)
    pass

async def main():
  start_time_Final = datetime.now()
  for device in devices:
    await asyncio.gather(openconnexion(device,username,password,commandes))
  totalFinal = datetime.now() - start_time_Final
  print (totalFinal)

asyncio.run(main())
Regards
Reply


Messages In This Thread
asyncio question - by fdFrench - Apr-20-2020, 10:31 AM
RE: asyncio question - by deanhystad - Apr-20-2020, 01:10 PM
RE: asyncio question - by fdFrench - Apr-20-2020, 01:46 PM
RE: asyncio question - by deanhystad - Apr-20-2020, 01:59 PM
RE: asyncio question - by fdFrench - Apr-20-2020, 02:35 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020