[英]How can I solve my IndexError issue so I can run my script continuously after wards?
我是 python 新手,我了解了這種新的自動化,旨在檢查網絡並說當您輸入 ip 的人到達時,我在開始時修復了幾個錯誤,但現在我被卡住了。
import sys
import subprocess
import os
from decouple import config
IP_NETWORK = config('IP_NETWORK')
IP_DEVICE = config('IP_DEVICE')
proc = subprocess.Popen(["ping", IP_NETWORK], stdout=subprocess.PIPE)
while True:
line = proc.stdout.readline()
if not line:
break
#the real code does filtering here
connected_ip = line.decode('utf-8').split()[3]
if connected_ip == IP_DEVICE:
subprocess.Popen(["say", "New just connected to the network ! "])
我試圖看看 split 是如何工作的,但我不明白
錯誤 :
file.py", line 15, in <module>
connected_ip = line.decode('utf-8').split()[3]
IndexError: list index out of range
也許line.decode('utf-8')
的輸出少於 4 個部分? 在這種情況下,會拋出out of range
錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.