定时关闭windows网络
import os
import time
from datetime import datetime, timedelta
import logging
import random
if __name__ == '__main__':
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
time_now = datetime.now()
time_shutdown = time_now.replace(hour=5, minute=54)
if time_shutdown < time_now:
time_shutdown = time_shutdown + timedelta(days=1)
sleep_secs = (time_shutdown-time_now).seconds
logging.error('>>>>> 将会在 {0} 关闭网络, 等待时间 {1} 秒'.format(time_shutdown.strftime('%Y-%m-%d %H:%M:%S'), sleep_secs))
time.sleep( sleep_secs )
logging.error('>>>>> 关闭网络')
os.system('ipconfig /release')
time_now = datetime.now()
time_start = time_now.replace(hour=8, minute=int(random.uniform(45, 58)))
if time_start < time_now:
time_start = time_start + timedelta(days=1)
sleep_secs = (time_start-time_now).seconds
logging.error('>>>>> 将会在 {0} 启用网络, 等待时间 {1} 秒'.format(time_start.strftime('%Y-%m-%d %H:%M:%S'), sleep_secs))
time.sleep( sleep_secs )
logging.error('启用网络')
os.system('ipconfig /renew')