执行Python代码时发生IndentationError

执行Python代码时发生IndentationError,python,python-2.7,python-3.x,Python,Python 2.7,Python 3.x,请通读上述代码,并确定在test=“ping”行中出现的错误,该行将获得一个indicationerror from os import popen from string import split, join from re import match import subprocess import os # execute the code and pipe the result to a string rtr_table = [elem.strip().split() for ele

请通读上述代码,并确定在test=“ping”行中出现的错误,该行将获得一个
indicationerror

from os import popen
from string import split, join
from re import match
import subprocess
import os
   # execute the code and pipe the result to a string
rtr_table = [elem.strip().split() for elem in popen("route print").read().split("Metric\n")[1].split("\n") if match("^[0-9]", elem.strip())]
#print "Active default gateway:", rtr_table[0][2]
x=rtr_table[0][2]
#x has a.a.a.a 
y = x[:x.rfind(".")]
# y has a.a.a
# in order to test in range of 1 to 255 for the default gateway
for i in range(1,255):
#ping by sending 1 packet and wait for 500 milli seconds 
test = "ping -n 1 -w 500 %s.%d" % (y,i)
process = subprocess.Popen(test, shell=True, stdout=subprocess.PIPE)
   # give it time to respond
process.wait()
   # optional check (0 --> success)
   #print process.returncode
   # read the result to a string
result_str = process.stdout.read()
   # test it ...
   # print result_str
#from arp we get mac address and corresponding ip address and state as we use '-a'
lines=os.popen('arp -a')
for line in lines:
   print line
sh-4.3$ python main.py                                                                                                                                                          
  File "main.py", line 20                                                                                                                                                       
    test = "ping -n 1 -w 500 %s.%d" % (y,i)                                                                                                                                     
       ^                                                                                                                                                                        
IndentationError: expected an indented block                                                                                                

您忘记了在下面的第一个for循环缩进

您应该在
for
语句之后缩进。看起来非常明显RTR_table=[elem.strip().split()(用于popen(“路由打印”).read().split(“度量\n”)[1]。split(“\n”)如果匹配(^[0-9]”,elem.strip())]indexer:列表索引超出范围我收到此错误
from os import popen
from string import split, join
from re import match
import subprocess
import os
   # execute the code and pipe the result to a string
rtr_table = [elem.strip().split() for elem in popen("route print").read().split("Metric\n")[1].split("\n") if match("^[0-9]", elem.strip())]
#print "Active default gateway:", rtr_table[0][2]
x=rtr_table[0][2]
#x has a.a.a.a 
y = x[:x.rfind(".")]
# y has a.a.a
# in order to test in range of 1 to 255 for the default gateway
for i in range(1,255):
    #ping by sending 1 packet and wait for 500 milli seconds 
    test = "ping -n 1 -w 500 %s.%d" % (y,i)
    process = subprocess.Popen(test, shell=True, stdout=subprocess.PIPE)
    # give it time to respond
    process.wait()
    # optional check (0 --> success)
    #print process.returncode
    # read the result to a string
    result_str = process.stdout.read()
    # test it ...
    # print result_str
    #from arp we get mac address and corresponding ip address and state as we use '-a'
lines=os.popen('arp -a')
for line in lines:
   print line