优化终端显示

This commit is contained in:
岛风 2024-01-08 17:12:53 +08:00
parent 4151b5796b
commit f5148e7d34

48
main.py
View File

@ -3,44 +3,58 @@ import os, time ,requests
def network_test():
try:
html = requests.get("https://www.baidu.com",timeout=5)
print("{} network 200 , ok".format(time.ctime()))
return True
except:
print("{} network 403 , down".format(time.ctime()))
return False
def network_restart():
print("正在重启en0..")
try:
os.system("ifconfig en0 down")
print("关闭en0...")
except:
print("关闭en0失败!")
return False
time.sleep(1)
try:
os.system("ifconfig en0 up")
print("启动en0...")
except:
print("启动en0失败!")
return False
return True
def ascii_tag():
sys_s = None
sta_s = None
if i > 0:
sys_s = "Down"
sta_s = "{} network 403 , down".format(time.ctime())
else:
sys_s = "Up"
sta_s = "{} network 200 , ok".format(time.ctime())
os.system("clear")
print(" _ _ _______ ")
print(" / \ / |_ |_ __ \ ")
print(" / _ \ __ _ `| |-' .--. | |__) | .---. ")
print(" / ___ \ [ | | | | | / .'`\ \ | __ / / /__\\ ")
print(" _/ / \ \_ | \_/ |,| |,| \__. |_| | \ \_| \__., ")
print("|____| |____|'.__.'_/\__/ '.__.'|____| |___|'.__.' ")
print("")
print("系统架构:{}\n异常次数:{}\n当前状态:{}\n最近记录:{}".format(os.name,j,sys_s,sta_s))
return True
def main():
global i ,j
i = 0
j = 0
while(1):
if not network_test():
i +=1
print(i)
ascii_tag()
if not network_test(): #网络异常
i +=1 #异常计数 3次失败重启
time.sleep(3)
else:
else: #网络正常
i = 0
time.sleep(10)
if (i > 2):
if not network_restart():
print("异常,程序退出!")
exit()
else:
print("完成!")
if (i > 2): #异常3次
if not network_restart(): #接口重启失败
input()
else: #接口重启完成
i = 0
time.sleep(10)