1 # -*- coding: utf-8 -*- 2 import datetime 3 import os 4 import threading 5 import time 6 7 import ntplib 8 import requests 9 10 11 def get_beijin_time(): 12 try: 13 url = 'https://beijing-time.org/' 14 # print(url) 15 request_result = requests.get(url=url) 16 # print(request_result.status_code) 17 18 if request_result.status_code == 200: 19 headers = request_result.headers 20 net_date = headers.get("date") 21 gmt_time = time.strptime(net_date[5:25], "%d %b %Y %H:%M:%S") 22 bj_timestamp = int(time.mktime(gmt_time) + 8 * 60 * 60) 23 ret = datetime.datetime.fromtimestamp(bj_timestamp) 24 # print("1=%s" % ret) 25 # ret = str(ret).rsplit(".")[0] 26 return ret 27 except Exception as exc: 28 # print(exc) 29 ret = datetime.datetime.now() 30 ret = str(ret).rsplit(".")[0] 31 # print("2=%s" % ret) 32 return ret 33 34 35 def xingqi_x(str_date): 36 """ 37 输入"2023-04-24 14:48:31" 38 返回星期1-7 39 """ 40 t = time.strptime(str_date, "%Y-%m-%d %H:%M:%S") 41 return 1 + t.tm_wday 42 43 44 def get_ntp_time(): 45 try: 46 ntp_client = ntplib.NTPClient() 47 response = ntp_client.request('time1.aliyun.com') 48 ret = datetime.datetime.fromtimestamp(response.tx_time) 49 ret2 = str(ret).rsplit(".")[0] 50 return ret2 51 except Exception as exc: 52 # print(exc) 53 ret = datetime.datetime.now() 54 ret = str(ret).rsplit(".")[0] 55 # print("2=%s" % ret) 56 return ret 57 58 59 def calc_time(bj_time): 60 return bj_time.split()[1] 61 62 63 def run_cmd(cmd_str='', echo_print=1): 64 """ 65 执行cmd命令,不显示执行过程中弹出的黑框 66 备注:subprocess.run()函数会将本来打印到cmd上的内容打印到python执行界面上,所以避免了出现cmd弹出框的问题 67 :param cmd_str: 执行的cmd命令 68 :return: 69 """ 70 from subprocess import run 71 # if echo_print == 1: 72 # print('执行cmd指令="{}"'.format(cmd_str)) 73 run(cmd_str, shell=True) 74 75 76 def run_cmd_Popen_fileno(cmd_string): 77 """ 78 执行cmd命令,并得到执行后的返回值,python调试界面输出返回值 79 :param cmd_string: cmd命令,如:'adb devices' 80 :return: 81 """ 82 import subprocess 83 84 print('运行cmd2指令:{}'.format(cmd_string)) 85 subprocess.Popen(cmd_string, shell=True, stdout=None, stderr=None).wait() 86 print("over") 87 return 88 89 90 def run_cmd_Popen_PIPE(cmd_string): 91 """ 92 执行cmd命令,并得到执行后的返回值,python调试界面不输出返回值 93 :param cmd_string: cmd命令,如:'adb devices"' 94 如"C:\Windows\SysWOW64\shutdown.exe /p" 95 :return: 96 """ 97 import subprocess 98 99 print('运行cmd3指令:{}'.format(cmd_string)) 100 return subprocess.Popen(cmd_string, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='gbk').communicate()[0] 101 102 103 def os_system(cmd_str): 104 """ 105 "C:\Windows\SysWOW64\shutdown.exe /p" 106 """ 107 os.system(cmd_str) 108 109 110 def check_shutdown(): 111 timer = threading.Timer(1.0, check_shutdown) 112 timer.start() 113 114 # bj_time = get_beijin_time() 115 ntp_time = get_ntp_time() 116 time_str = calc_time(ntp_time) 117 xing_x = xingqi_x(ntp_time) 118 119 # print("now:%s, xing:%d" % (ntp_time, xing_x)) 120 121 # run_cmd_Popen_fileno("D:\JiuJiu\_timer_daka\myshutdown_now.bat") 122 123 if 3 == xing_x or 5 == xing_x: 124 # if "15:20:00" <= time_str <= "23:59:59": 125 if "20:30:00" <= time_str <= "23:59:59": 126 os_system("C:\Windows\SysWOW64\shutdown.exe /p") 127 elif 1 == xing_x or 2 == xing_x or 4 == xing_x: 128 if "18:00:00" <= time_str <= "23:59:59": 129 os_system("C:\Windows\SysWOW64\shutdown.exe /p") 130 else: 131 if "18:00:00" <= time_str <= "23:59:59": 132 os_system("C:\Windows\SysWOW64\shutdown.exe /p") 133 134 if "00:00:00" <= time_str <= "09:00:00": 135 os_system("C:\Windows\SysWOW64\shutdown.exe /p") 136 137 138 if __name__ == '__main__': 139 check_shutdown() 140 # print(get_beijin_time()) 141 # print(get_ntp_time())
标签:return,cmd,自动关机,ret,下班,str,time,print From: https://www.cnblogs.com/LiuYanYGZ/p/17362358.html