MacserverAutoRestartNetwork/main.py

51 lines
1.1 KiB
Python
Raw Normal View History

2024-01-08 10:51:35 +08:00
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():
2024-01-08 11:11:58 +08:00
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
2024-01-08 10:51:35 +08:00
return True
def main():
i = 0
while(1):
if not network_test():
i +=1
print(i)
time.sleep(3)
else:
time.sleep(10)
if (i > 2):
2024-01-08 11:11:58 +08:00
if not network_restart():
print("异常,程序退出!")
exit()
else:
print("完成!")
i = 0
time.sleep(10)
2024-01-08 10:51:35 +08:00
if __name__ == "__main__":
main()