首页 > 其他分享 >下班自动关机

下班自动关机

时间:2023-04-28 15:46:49浏览次数:28  
标签:return cmd 自动关机 ret 下班 str time print

  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

相关文章

  • 不坑盒子(Office,WPS接入人工智能)助你高效办公,掐点准时下班回家。
    不坑盒子很多朋友在工作过程中需要对Word文档进行编辑处理,如果想让Word排版更有效率可以试试小编带来的这款不坑盒子软件,这是一个非常好用的插件工具,专门应用在Word文档中,支持Office2010以上的版本,用户可以借助工具快速实现排版操作,还支持仿手写功能,简单实用,同时还支持使用ChatG......
  • 程序员下班做什么副业好?实战方法新手小白可上手!1000多字总结
    以前我就是个程序员,所以,现在有做副业的想法是很不错的,可以多点收入,而且以下方法即使不是程序员,新手小白也可以干。我来分享一下做副业的经验和看法;现在我已经往运营方向发展了。作为一个网站开发工程师,我在上家公司干了3年的活,做过javaweb,前端html、js、css,asp.net we......
  • 【故障公告】下班前的一场暴风雨,爬虫爬至园宕机
    下班前的一场暴风雨,让园子一片狼藉。顶着暴风雨,加了服务器,但无济于事。情急之中,断蛛求生立转机。今天下班前的17:00~17:30左右,身份未明的爬虫暴风雨般地袭击园子,造成数......
  • windows 10 设置每天自动关机任务
    1.设置系统每天18:30自动待机;设置系统在每周六和每周日的7:05自动待机schtasks/create/tnmy-standby /trrundll32.exepowrprof.dll,SetSuspendState/scdaily/s......
  • 学会规则引擎Drools,让你早点下班
    前言现在有这么个需求,网上购物,需要根据不同的规则计算商品折扣,比如VIP客户增加5%的折扣,购买金额超过1000元的增加10%的折扣等,而且这些规则可能随时发生变化,甚至增加新的规......
  • 电脑自动关机怎么设置(方法)
    电脑自动关机怎么设置,我们可以通过卓越电脑定时关机软件来实现电脑定时自动关机。卓越电脑定时关机软件主要功能:1、可设置电脑定时自动关机,定时重启,定时注销,定时锁屏,定......
  • 分享一个可以早点下班的开发小技巧
    这次来跟大家分享一下让大家早下班的工具。首先要声明一下,分享的工作,是为了大部分人,而不是“某一个人”,不喜勿喷哈!介绍的就是这两年很火的低代码平台,网上两种观点,第一种人......
  • 今天是2023年2月24日,周五下班前摸鱼中
    当前我在听一个台湾广播,主持人说他们马上要放4天假,一会说国语,一会说闽南语,好好玩。突然想起来,在我上初中的时候,我有一个随身听,是我们村一个小伙伴抵账给我的,当时他借了我1......
  • Mac mini 自动关机 bug All In One
    Macmini自动关机bugAllInOne问题描述发现Macmini夜里突然自动关机了,按了开机键也没有反应,导致Macmini无法开机❌故障原因分析浏览器开太多标签页面,导......
  • 手上有了这些工具,明天争取6点下班了!
    一个优秀的开发者,一定是会利用各种工具来提升自己的开发效率。这段时间,我开始分享优质提升开发效率的工具,集成效率工具,开源集成网站等等,朋友们一直在催更,我知道你很急,但是......