1 回答
TA贡献1820条经验 获得超10个赞
缩进肯定会让你的代码很难阅读。我怀疑您看到多个列表的原因是您的print呼叫在for循环内。以下是解决此问题的方法:
def snmpWALK(request):
all_lists = []
if request.method=='GET':
host= 'localhost'
oid = '1.3.6.1.2.1.1.9.1.2'
for t in nextCmd(
SnmpEngine(),
CommunityData('public'),
UdpTransportTarget((host, 161)),
ContextData(),
ObjectType(ObjectIdentity(oid)),
lookupMib=False,
lexicographicMode=False):
# I broke this up for purposes of formatting
# on SO. normally, I would just stick these in
# the for loop above.
errorIndication, errorStatus = t[0], t[1]
errorIndex, varBinds = t[2], t[3]
if errorIndication:
print(errorIndication, file=sys.stderr)
break
elif errorStatus:
print('%s at %s' % (
errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex) - 1][0] or '?'
), file=sys.stderr)
break
else:
MyList = []
for varBind in varBinds:
thing='%s = %s' % varBind
MyList.append(thing)
# this is within the for loop!!!
print(MyList)
all_lists.append(MyList)
return render(request, 'snmpWALK.html', {'MyList': all_lists})
一般来说,对于我们这些喜欢帮助解决 SO 的人来说,你的代码很难阅读,因为 (1) 它的缩进不正确(你可以看到breakOP 中的语句)和 (2) 它没有遵循 PEP8。YMMV 如果您想遵循这些约定/建议,那么这样做会更容易帮助您。
添加回答
举报