我一直在尝试编写脚本代码,以便自动ping一些98%的正常运行时间网络,如谷歌或Facebook等。虽然我尝试运行它时代码似乎很好但我在某些else
上收到错误声明。
我认为我的if
和elif
有点搞砸了,所以有人可以伸出援助之手吗?
以下是代码:
import datetime
import os
import time
now = datetime.time()
print ('Starting network availiability test at %s' % (now))
time.sleep(2)
print ('The availiable servers for testing network availiabilty are: 1.Google 2.Apple 3.Facebook')
time.sleep(1)
host = raw_input('Please select which server should we ping in order to see if your network is up ?')
hostname1 = "google.com"
hostname2 = "apple.com"
hostname3 = "facebook.com"
if host == 1 :
print ('We are trying to reach Google, please wait')
resp = os.system('ping -n 1' + hostname1)
if resp == 0:
print ('The server is reachable, so your network should be fine')
else :
print ('Google is not reachable. Your network seems down')
elif host == 2 :
print ('We are trying to reach Apple, please wait')
resp1 = os.system('ping -n 1' + hostname2)
if resp1 == 0:
print ('The server is reachable, so your network should be fine')
else :
print ('Apple is not reachable. So your network seems down.')
elif host == 3 :
print ('We are trying to reach Facebook, please wait')
resp2 = os.system('ping -n 1' + hostname3)
if resp2 == 0:
print ('The server is reachable, so your network should be fine')
else :
print ('Facebook is not reachable. Your network seems down.')