2

I'm currently working on a SNMP module in Python 3 (Python 3.1.3) based on PySnmp to easily send GET/WALK SNMP queries from other programs. This is mainly for fun/learning.

When querying an existing OID, I get a tuple such as :

(ObjectName(1.3.6.1.2.1.1.7.0), Integer(72))

which I can read using a "for" construct.

However, when querying a non-existing OID (which is what I do for unit testing), I get :

(ObjectName(2.3.4.5.6.7.8), NoSuchObject('b'''))

How can I differentiate the "normal" case, where the 2nd element of the tuple is an integer/string/(other?) and the "error" case where this 2nd element is a 'NoSuchObject' ?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Httqm
  • 799
  • 7
  • 13

2 Answers2

1

like here Determine the type of an object?

type(your_tuple[1]) == NoSuchObject

or

isinstance(your_tuple[1], NoSuchObject)
Community
  • 1
  • 1
andrjas
  • 260
  • 1
  • 5
  • When I tried these, it failed miserably : `NameError: global name 'NoSuchObject' is not defined` Then I realized my code had no clue what 'NoSuchObject' was. It worked like a charm after I did : `from pysnmp.proto.rfc1905 import NoSuchObject` Got it, thx ! – Httqm Apr 23 '13 at 21:07
1

Another approach is to match ASN.1 types of noSuchObject and the value part of SNMP response var-bind tuple:

>>> from pysnmp.proto import rfc1905
>>> rfc1905.noSuchObject.isSameTypeWith(rfc1905.noSuchObject)
True
>>> rfc1905.noSuchObject.isSameTypeWith(rfc1905.noSuchInstance)
False
>>> rfc1905.noSuchObject.isSameTypeWith(varBind[1]) # varBind from SNMP response
False
Pooh
  • 244
  • 1
  • 2