用于bandwith阈值索引器的Python函数错误:列表索引超出范围错误

用于bandwith阈值索引器的Python函数错误:列表索引超出范围错误,python,Python,我目前正试图通过以下教程使我的Raspberry PI 2与亚马逊的AWS物联网协同工作: 对于此功能,应发出超过网络带宽阈值的警告: def monspeed(): c = checkspeed(-1) if c[2] > 2000000: # customize the interface/speed to trigger the warning awsmsg = {'state':{ 'reported': {'warning': 'speed' ,

我目前正试图通过以下教程使我的Raspberry PI 2与亚马逊的AWS物联网协同工作:

对于此功能,应发出超过网络带宽阈值的警告:

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
回溯(最近一次呼叫最后一次):
文件“/netmon.py”,第158行,在
monspeed()
文件“/netmon.py”,第98行,在monspeed中
如果c[2]>2000000:#定制接口/速度以触发警告
索引器:列表索引超出范围

错误表明
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
的类型和值


使用此策略,您可以看到可能触发错误的变量发生了什么,从而获得有用的信息,以找出错误所在。

欢迎访问此站点!请提供更多信息,以便人们能够更好地帮助您。在这种情况下,至少需要
checkspeed()
的内容。看见