我正在尝试通过遵循本教程使我的Raspberry PI 2与亚马逊的AWS IoT协同工作:https://www.hackster.io/phantom-formula-e97912/network-monitoring-with-aws-iot-b8b57c?ref=platform&ref_id=425_trending___&offset=12
对于此功能,应发出超出网络带宽阈值的警告:
def monspeed():
c = checkspeed(-1)
if c[2] > 2000000: # customize the interface/speed to trigger the warning
awsmsg = {'state':{ 'reported': {'warning': 'speed' , 'result': c}}}
payload = json.dumps(awsmsg)
print (awsmsg)
client.publish(topic,payload,qos,retain)
我收到此错误消息:
Traceback (most recent call last):
File "./netmon.py", line 158, in <module>
monspeed()
File "./netmon.py", line 98, in monspeed
if c[2] > 2000000: # customize the interface/speed to trigger the warning
IndexError: list index out of range
答案 0 :(得分:0)
错误表明c
对象中包含的元素少于3个。当它接收-1作为参数时,它直到checkspeed
的输出。
为了查看c
发生了什么,请使用这样的try / except块:
try:
if c[2] > 2000000:
awsmsg = {'state':{ 'reported': {'warning': 'speed' , 'result': c}}}
payload = json.dumps(awsmsg)
print (awsmsg)
client.publish(topic,payload,qos,retain)
except IndexError as e:
print 'What is "c"?', type(c)
print 'Values in "c":', c
raise e
执行此操作,您可以在触发错误时检查变量c
的类型和值。
使用这种策略,您可以看到可能触发错误的变量会发生什么,从而获得有助于找出错误行为的信息。