我安装了Anaconda2.5,它给了我Python 2.7。我安装了最新的PySNMP。最初,我尝试使用Python 2.6在另一台服务器上运行Python脚本并获得TypeError(见下文)。我认为一定是因为我的Python版本,可能是PySNMP改变了一些东西,但是当我运行他们的示例代码(见下文)时,它返回了完全相同的错误。我很困惑为什么他们的示例代码会返回错误并且只能怀疑Anaconda安装PySNMP会出错?
这是示例代码:
#!/root/anaconda2/python
from pysnmp.hlapi import *
for (errorIndication,
errorStatus,
errorIndex,
varBinds) in nextCmd(SnmpEngine(),
CommunityData('public', mpModel=0),
UdpTransportTarget(('demo.snmplabs.com', 161)),
ContextData(),
ObjectType(ObjectIdentity('IF-MIB', 'ifDescr')),
ObjectType(ObjectIdentity('IF-MIB', 'ifType')),
ObjectType(ObjectIdentity('IF-MIB', 'ifMtu')),
ObjectType(ObjectIdentity('IF-MIB', 'ifSpeed')),
ObjectType(ObjectIdentity('IF-MIB', 'ifPhysAddress')),
ObjectType(ObjectIdentity('IF-MIB', 'ifType')),
lexicographicMode=False):
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(' = '.join([x.prettyPrint() for x in varBind]))
这是我的错误:
Traceback (most recent call last): File "get_aqx_intersite_bw.py", line 8, in varBinds) in nextCmd(SnmpEngine(), File "/root/anaconda2/lib/python2.7/site-packages/pysnmp/entity/engine.py", line 61, in __init__ self.msgAndPduDsp = MsgAndPduDispatcher() File "/root/anaconda2/lib/python2.7/site-packages/pysnmp/proto/rfc3412.py", line 29, in __init__ 'SNMP-TARGET-MIB', 'SNMP-USER-BASED-SM-MIB' File "/root/anaconda2/lib/python2.7/site-packages/pysnmp/smi/builder.py", line 337, in loadModules self.loadModule(modName, **userCtx) File "/root/anaconda2/lib/python2.7/site-packages/pysnmp/smi/builder.py", line 306, in loadModule 'MIB module \"%s\" load error: %s' % (modPath, traceback.format_exception(*sys.exc_info())) pysnmp.smi.error.MibLoadError: MIB module "/root/anaconda2/lib/python2.7/site-packages/pysnmp/smi/mibs/SNMPv2-MIB.pyc" load error: ['Traceback (most recent call last):\n', ' File "/root/anaconda2/lib/python2.7/site-packages/pysnmp/smi/builder.py", line 301, in loadModule\n exec(modData, g)\n', ' File "/home/ilan/minonda/envs/_build/lib/python2.7/site-packages/pysnmp/smi/mibs/SNMPv2-MIB.py", line 26, in \n', ' File "/root/anaconda2/lib/python2.7/site-packages/pysnmp/proto/rfc1902.py", line 201, in subtype\n self, value, implicitTag, explicitTag, subtypeSpec\n', 'TypeError: subtype() takes at most 2 arguments (5 given)\n']
答案 0 :(得分:0)
它必须与不兼容的pysnmp / pyasn1版本有关。
根据您的情况,您可能希望将两个软件包推送到最新的PyPI版本(pip install --upgrade pysnmp pyasn1
),或者,如果您遇到当前的pyasn1版本,请将pysnmp降级到4.3.10。