summaryrefslogtreecommitdiff
path: root/examples/v3arch/asyncio/manager/cmdgen/getbulk-v2c.py
diff options
context:
space:
mode:
authorelie <elie>2014-11-16 16:14:37 +0000
committerelie <elie>2014-11-16 16:14:37 +0000
commitedff85c75a5ba51c6824f6d09ec55986682db335 (patch)
treec0216f83de60b93ef9c24ad801a47e2a14308b6e /examples/v3arch/asyncio/manager/cmdgen/getbulk-v2c.py
parent481200dc56925509a54b89c28d054ba28f4e437c (diff)
downloadpysnmp-edff85c75a5ba51c6824f6d09ec55986682db335.tar.gz
asyncio-backed SNMP Applications APIs redesigned for better usability in
form of coroutines
Diffstat (limited to 'examples/v3arch/asyncio/manager/cmdgen/getbulk-v2c.py')
-rw-r--r--examples/v3arch/asyncio/manager/cmdgen/getbulk-v2c.py87
1 files changed, 45 insertions, 42 deletions
diff --git a/examples/v3arch/asyncio/manager/cmdgen/getbulk-v2c.py b/examples/v3arch/asyncio/manager/cmdgen/getbulk-v2c.py
index 0b40bd0..54c0972 100644
--- a/examples/v3arch/asyncio/manager/cmdgen/getbulk-v2c.py
+++ b/examples/v3arch/asyncio/manager/cmdgen/getbulk-v2c.py
@@ -53,57 +53,60 @@ config.addTargetAddr(
'my-creds'
)
-# Error/response receiver
-def cbFun(cbCtx):
- (snmpEngine, errorIndication,
- errorStatus, errorIndex, varBindTable, status) = cbCtx.result()
- if errorIndication:
- print(errorIndication)
- elif errorStatus:
- print('%s at %s' % (
- errorStatus.prettyPrint(),
- errorIndex and varBindTable[-1][int(errorIndex)-1][0] or '?'
- )
- )
- else:
- for varBindRow in varBindTable:
- for oid, val in varBindRow:
- print('%s = %s' % (oid.prettyPrint(), val.prettyPrint()))
-
- # Stop mainloop when we are done walking (optional)
- for oid, val in varBindRow:
- if not val.isSameTypeWith(rfc1905.endOfMibView):
- # Re-create future for next GETNEXT iteration
- future = asyncio.Future()
- future.add_done_callback(cbFun)
+@asyncio.coroutine
+def snmpOperation(snmpEngine, target, contextEngineId, contextName,
+ nonRepeaters, maxRepetitions, varBinds):
+ initialVarBinds = varBinds
+ while varBinds:
+ ( snmpEngine,
+ errorIndication,
+ errorStatus,
+ errorIndex,
+ varBindTable ) = yield from cmdgen.BulkCommandGenerator().sendVarBinds(
+ snmpEngine,
+ target,
+ contextEngineId,
+ contextName,
+ nonRepeaters,
+ maxRepetitions,
+ varBinds
+ )
- # This also indicates that we wish to continue walking
- status['future'] = future
- return
+ if errorIndication:
+ print(errorIndication)
+ break
+ elif errorStatus:
+ print('%s at %s' % (
+ errorStatus.prettyPrint(),
+ errorIndex and varBindTable[-1][int(errorIndex)-1][0] or '?'
+ )
+ )
+ break
+ else:
+ for varBindRow in varBindTable:
+ for oid, val in varBindRow:
+ print('%s = %s' % (oid.prettyPrint(), val.prettyPrint()))
+ errorIndication, varBinds = cmdgen.getNextVarBinds(
+ initialVarBinds, varBindRow
+ )
+
# This also terminates internal timer
config.delTransport(
snmpEngine,
udp.domainName
)
-
- # Stop mainloop on SNMP error (optional)
+
loop.stop()
-# Prepare request to be sent, receive an asyncio future object
-future = cmdgen.BulkCommandGenerator().sendVarBinds(
- snmpEngine,
- 'my-router',
- None, '', # contextEngineId, contextName
- 0, 25, # non-repeaters, max-repetitions
- ( ('1.3.6.1.2.1.1', None), ('1.3.6.1.4.1.1', None) )
+loop.run_until_complete(
+ snmpOperation(
+ snmpEngine,
+ 'my-router',
+ None, '', # contextEngineId, contextName
+ 0, 25, # nonRepeaters, maxRepetitions
+ ( ('1.3.6.1.2.1.1', None), ('1.3.6.1.2.1.11', None) )
+ )
)
-# Register error/response receiver function on future
-future.add_done_callback(cbFun)
-
-# Run asyncio main loop
-loop.run_forever()
-
-# Clear the event loop
loop.close()