Reuse getCmd object in pysnmp

微笑、不失礼 提交于 2019-12-25 08:46:13

问题


In the pysnmp documentation there is a getCmd class, I was wondering if it was possible to just instantiate the class once and reuse it at a later point by passing it new oids. I am not sure if the getCmd class exposes methods to allow me to change the oids.

http://pysnmp.sourceforge.net/docs/hlapi/asyncore/sync/manager/cmdgen/getcmd.html


回答1:


The getCmd name is referring to a function, not a class. Technically, it is a generator, but that is not important here.

It is cheap to call *Cmd() because all the heavy lifting and state management is done on the SnmpEngine object (first argument to getCmd). Thus it is important, from performance standpoint, to keep SnmpEngine object as persistent as possible.

>>> from pysnmp.hlapi.asyncore import *
>>> snmpEngine = SnmpEngine()
>>> for oid in ['1.3.6.1.2.1.2.2.1.8.1', '1.3.6.1.2.1.2.2.1.8.2']:
...     g = getCmd(snmpEngine,
...                CommunityData('public'),
...                UdpTransportTarget(('demo.snmplabs.com', 161)),
...                ContextData(),
...                ObjectType(ObjectIdentity(oid)))
>>>     print(next(g))


来源:https://stackoverflow.com/questions/34654132/reuse-getcmd-object-in-pysnmp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!