下班自动锁屏
部分摘自:https://blog.csdn.net/zh6526157/article/details/121775954
部分摘自:https://zhuanlan.zhihu.com/p/509537015
rundll32详解:https://www.docin.com/p-1514384389.html
rundll32详解:http://www.nndssk.com/rjwt/160953kedzdj.html
# -*- coding: utf-8 -*- import datetime import os import threading import time import ntplib import requests def get_beijin_time(): try: url = 'https://beijing-time.org/' # print(url) request_result = requests.get(url=url) # print(request_result.status_code) if request_result.status_code == 200: headers = request_result.headers net_date = headers.get("date") gmt_time = time.strptime(net_date[5:25], "%d %b %Y %H:%M:%S") bj_timestamp = int(time.mktime(gmt_time) + 8 * 60 * 60) ret = datetime.datetime.fromtimestamp(bj_timestamp) # print("1=%s" % ret) # ret = str(ret).rsplit(".")[0] return ret except Exception as exc: # print(exc) ret = datetime.datetime.now() ret = str(ret).rsplit(".")[0] # print("2=%s" % ret) return ret def xingqi_x(str_date): """ 输入"2023-04-24 14:48:31" 返回星期1-7 """ t = time.strptime(str_date, "%Y-%m-%d %H:%M:%S") return 1 + t.tm_wday def get_ntp_time(): try: ntp_client = ntplib.NTPClient() response = ntp_client.request('time1.aliyun.com') ret = datetime.datetime.fromtimestamp(response.tx_time) ret2 = str(ret).rsplit(".")[0] return ret2 except Exception as exc: # print(exc) ret = datetime.datetime.now() ret = str(ret).rsplit(".")[0] # print("2=%s" % ret) return ret def calc_time(bj_time): return bj_time.split()[1] def run_cmd(cmd_str='', echo_print=1): """ 执行cmd命令,不显示执行过程中弹出的黑框 备注:subprocess.run()函数会将本来打印到cmd上的内容打印到python执行界面上,所以避免了出现cmd弹出框的问题 :param cmd_str: 执行的cmd命令 :return: """ from subprocess import run # if echo_print == 1: # print('执行cmd指令="{}"'.format(cmd_str)) run(cmd_str, shell=True) def run_cmd_Popen_fileno(cmd_string): """ 执行cmd命令,并得到执行后的返回值,python调试界面输出返回值 :param cmd_string: cmd命令,如:'adb devices' :return: """ import subprocess print('运行cmd2指令:{}'.format(cmd_string)) subprocess.Popen(cmd_string, shell=True, stdout=None, stderr=None).wait() print("over") return def run_cmd_Popen_PIPE(cmd_string): """ 执行cmd命令,并得到执行后的返回值,python调试界面不输出返回值 :param cmd_string: cmd命令,如:'adb devices"' 如"C:\Windows\SysWOW64\shutdown.exe /p" :return: """ import subprocess print('运行cmd3指令:{}'.format(cmd_string)) return subprocess.Popen(cmd_string, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='gbk').communicate()[0] def os_system(cmd_str): """ "C:\Windows\SysWOW64\shutdown.exe /p" """ os.system(cmd_str) def os_shutdown(): os_system("C:/Windows/SysWOW64/shutdown.exe /p") # 故意把os.sep写为linux风格 def os_lock(): os_system("C:/Windows/System32/rundll32.exe user32.dll,LockWorkStation") # 故意把os.sep写为linux风格 def os_do(): os_lock() def check_shutdown(): timer = threading.Timer(1.0, check_shutdown) timer.start() # bj_time = get_beijin_time() ntp_time = get_ntp_time() time_str = calc_time(ntp_time) xing_x = xingqi_x(ntp_time) print("now:%s, xing:%d" % (ntp_time, xing_x)) # run_cmd_Popen_fileno("D:\JiuJiu\_timer_daka\myshutdown_now.bat") if 3 == xing_x or 5 == xing_x: # if "15:20:00" <= time_str <= "23:59:59": if "18:02:00" <= time_str <= "23:59:59": os_do() elif 1 == xing_x or 2 == xing_x or 4 == xing_x: if "20:30:00" <= time_str <= "23:59:59": os_do() else: if "17:00:00" <= time_str <= "23:59:59": os_do() if "00:00:00" <= time_str <= "09:00:00": os_do() if __name__ == '__main__': check_shutdown() # print(get_beijin_time()) # print(get_ntp_time())