代碼片斷1

定時關閉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')
相關文章
相關標籤/搜索