IP地址必须指定?在Python 2.7

IP地址必须指定?在Python 2.7

问题描述:

继承人运行在Windows 10IP地址必须指定?在Python 2.7

import os 

with open('test.txt') as f: 
    for line in f: 
     response = os.system("ping -c 1" + line) 
     if response == 0: 
       print(line, "is up!") 
     else: 
       print(line, "is down!") 

test.txt文件我的代码包含了一些随机的IP的,代码运行,但是当它把它送给了必须指定的IP地址的邮件。我的问题是我不知道如何在脚本中做到这一点。当我使用常规命令promt并执行ping -c 1 google.com时,它会运行,但是使用上述需要指定google.com的python脚本从文本文件中读取它。

Q1:指定ip是什么意思,我该怎么做? Q2:我应该以不同的方式编写我的代码导入不同的模块吗?

+0

看看'for'循环中的缩进。 (提示:没有:))。有条件的'if'语句也是如此。 – Lix

+0

~~首先,你是否试过打印出“线”的内容?你可以更好地了解故障发生的地点~~假装它已经被击穿了是的,那样做^ –

+0

是的,我只是试着打印一行,并且单独给出了它们之间的空间。我也修正了这里的缩进问题,我的代码中没有正确的代码。 – Cry2Senpai

import os 

with open('test.txt') as f: 

for line in f: 

    response = os.system("ping -c 1 " + line.strip()) 

    if response == 0: 

    print(line, "is up!") 

    else: 

    print(line, "is down!") 

剥离文件末尾的换行符并在ping命令中添加一个空格。

+0

啊看着我的代码的所有迭代我看到我开始打字,太空谢谢你。 – Cry2Senpai