簡體   English   中英

如何解決我的 IndexError 問題,以便我可以在病房之后連續運行我的腳本?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM