Python pysnmp:重用迭代器遍历多个OID?

Python pysnmp:重用迭代器遍历多个OID?,python,network-programming,snmp,pysnmp,Python,Network Programming,Snmp,Pysnmp,我正在使用pysnmp查询多个oid范围。我需要使用nxtCmd,因为我需要遍历这些地址,例如,所有的IF地址。但是,当我在行走时使用lexicographicMode=False来防止跨越OID障碍时,我无法使用相同的迭代器使用“send”查询新的范围,因为我得到了StopIteration错误(迭代器已耗尽)。解决方案是每次实例化一个新的迭代器吗?我是不是处理得不对 代码: 第一个循环起作用,send命令产生StopIteration异常是,如果迭代器已耗尽,则无法重用它 然而,即使是迭代器

我正在使用pysnmp查询多个oid范围。我需要使用nxtCmd,因为我需要遍历这些地址,例如,所有的IF地址。但是,当我在行走时使用lexicographicMode=False来防止跨越OID障碍时,我无法使用相同的迭代器使用“send”查询新的范围,因为我得到了StopIteration错误(迭代器已耗尽)。解决方案是每次实例化一个新的迭代器吗?我是不是处理得不对

代码:


第一个循环起作用,send命令产生StopIteration异常

是,如果迭代器已耗尽,则无法重用它

然而,即使是迭代器和生成器,也只有生成器支持该方法


我会首先检查
nextCmd
是否确实返回了一个生成器,如果返回了,请查看本文:,它会给您一个示例。

试试
itertools.tee
iterator = nextCmd(SnmpEngine(),
                          CommunityData('public'),
                          UdpTransportTarget(('localhost', 161)),
                          ContextData(),
                          ObjectType(ObjectIdentity('1.3.6.1.2.1.4.20.1.1')),
                          lexicographicMode=False)

for (errorIndication,
     errorStatus,
     errorIndex,
     varBinds) in iterator:

    if errorIndication:
        print(errorIndication)
        break
    
    elif errorStatus:
        print('%s at %s' % (errorStatus.prettyPrint(),
                            errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
        break
    
    else:
        for varBind in varBinds:
            print(type(varBind))
            print(' = '.join([x.prettyPrint() for x in varBind]))

errorIndication, errorStatus, errorIndex, varBinds = iterator.send(ObjectType(ObjectIdentity('1.3.6.1.2.1.4.20.1.1')))
print(varBinds[1].prettyPrint)