python <shut.py>
import ntplib
from datetime import datetime, timezone
import time
import os
import easygui
def get_network_time():
time.sleep(3)
"""从网络时间服务器获取时间"""
client = ntplib.NTPClient()
response = client.request('pool.ntp.org')
utc_time = datetime.utcfromtimestamp(response.tx_time)
beijing_time = utc_time + timedelta(hours = 8)
return beijing_time
local = datetime.now()
if local.hour >=0 and local.hour<7:
print("当前时间处于0点至9点之间")
os.system('shutdown /a')
os.system('shutdown /s /t 60')
easygui.msgbox("现在是休息时间,电脑即将关机,请你马上睡觉!!", "提示")
easygui.msgbox("电脑即将关机!", "提示")
else:
easygui.msgbox("当前时间不处于0点至7点之间=>local:[{}]".format(local.hour), "提示")
os.system('shutdown /s /t 9000')
time.sleep(10)
while True:
print("每隔3小时,执行关机")
easygui.msgbox("电脑将在2.5小时后关闭,请注意休息~~", "提示")
time.sleep(10800)
os.system('shutdown /a')
os.system('shutdown /s /t 60')
'''
try:
# 获取网络时间
current_time = get_network_time()
# 提取小时数
hour = current_time.hour
# 判断是否处于0点至7点之间
if 0 <= hour < 7:
print("当前时间处于0点至7点之间")
os.system('shutdown /a')
os.system('shutdown /s /t 60')
easygui.msgbox("现在是休息时间,电脑即将关机,请你马上睡觉!!", "提示")
easygui.msgbox("电脑即将关机!", "提示")
else:
easygui.msgbox("当前时间不处于0点至7点之间=>remote[{}]".format(hour), "提示")
os.system('shutdown /s /t 9000')
except:
print('网络异常!!')
'''
vbe文件
Set ws = WScript.createObject("WScript.Shell")
ws.run "D:/data\Python/cmd.bat",0
<cmd.bat>
@echo off
title 执行Python脚本
c: & cd c:\Users\Administrator\AppData\Local\Programs\Python\Python37
python "D:\data\Python\shut.py" >>"D:\data\Python\%date:~0,4%-%date:~5,2%-%date:~8,2% %time:~0,2%-%time:~3,2%-%time:~6,2%.txt"
::pause
计划任务,批处理,避免冲突
@echo off
::由于shutdown不能执行多次,所以先取消后,再次执行!
shutdown /a
shutdown /s /t 30