dota监測

执行环境:win7 32位.

python版本号:3.4.1

因为用到了一些win32api,这些并不是python标准库自带的,所以你须要先去下载pywin32模块.去http://sourceforge.net/projects/pywin32/files/pywin32/Build%20219/找到相应你的系统及python版本号的包,一路next安装就可以.

复制代码
#coding=gbk
import win32com.client
import time
import os


def IsExsit(processName):
    WMI = win32com.client.GetObject('winmgmts:')
    processCodeCov = WMI.ExecQuery(
        'select * from Win32_Process where Name="%s"' % processName)
    if len(processCodeCov) > 0:
        return 1
    else:
        return 0

oldStatus = 0
newStatus = 0   #0代表进程不存在.1代表存在
totalTime = 0

def ShutDown(fp):
    cmd = "cmd.exe /k shutdown -s -t 0"
    structTime = time.localtime(time.time())
    currentTime = time.strftime('%Y-%m-%d %H:%M:%S', structTime)
    info = "关机:" + currentTime + "
"
    fp.write(info)
    os.system(cmd)

def Handle(processName,fp):
    global oldStatus, newStatus,totalTime
    newStatus = IsExsit(processName)
    structTime = time.localtime(time.time())
    currentTime = time.strftime('%Y-%m-%d %H:%M:%S', structTime)
    print(oldStatus, newStatus,currentTime)
    
    #更新进程执行总时间.
    if newStatus == 1:
        totalTime += 60              
        if totalTime >= 60*60*3:      #超过3小时则关机
            ShutDown(fp)

    #在进程状态改变时记录到文件里
    if (oldStatus != newStatus): 
        oldStatus = newStatus  
        structTime = time.localtime(time.time())
        strTime = time.strftime('%Y-%m-%d %H:%M:%S', structTime)
        if newStatus:
            strTmp = processName + "开启"
        else:
            strTmp = processName + "关闭"
        info = strTime + "********" + strTmp + "
"
        fp.write(info)
        #fp.close()
    else:                              #
        pass


if __name__ == '__main__':
    fp = open("records.txt", "a+")
    structTime = time.localtime(time.time())
    currentTime = time.strftime('%Y-%m-%d %H:%M:%S', structTime)
    info = "開始监控:" + currentTime + "
"
    fp.write(info)
    while True:
        Handle('war3.exe',fp)
        #5分钟检測一次
        time.sleep(60)
复制代码
【推广】 免费学中医,健康全家人
原文地址:https://www.cnblogs.com/zhchoutai/p/8503797.html