etingof / pysnmp

Python SNMP library
http://snmplabs.com/pysnmp/
BSD 2-Clause "Simplified" License
574 stars 192 forks source link

Q: convert SNMP SMI types to Python types #262

Open fried-sausage opened 5 years ago

fried-sausage commented 5 years ago

Considering this example, I'd like to convert values in varBinds to native python types (so I could json.dumps those values later).

Is there a canonical way to do that which would cover most cases, or should I implement my own mapping like DisplayString -> str, Integer -> int etc.?

etingof commented 5 years ago

There is no canonical way for such conversion. I think having your own map like:

{
  Integer.tagSet: int,
  OctetString.tagSet: str,
  ...
}

might make sense. But it really depends on your end goal.

The thing is that not all types map universally. Take transport endpoint as an example - should it always be mapped into a string or a tuple or a dict or something else? That largely depends on the use-case, I think.

On top of that, there is the TEXTUAL-CONVENTION modifier which basically turns everything into a string. The map above won't distinguish values of the same base type, but different textual conventions. If you need that, perhaps you should anchor on the original MIB classes, oh...