zoukankan      html  css  js  c++  java
  • 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)
    复制代码
  • 相关阅读:
    App调试的几个命令实践【转】
    解决sdk更新时候报错 http://dl-ssl.google.com/android上不去,链接拒绝
    fastjson序列化排序问题
    Java中的四种引用
    equal&==&hashcode
    ThreadPool线程池的关注点
    JVM的本地方法栈
    JVM的堆分配
    JVM的类装载子系统
    JVM的数据类型
  • 原文地址:https://www.cnblogs.com/zhchoutai/p/8503797.html
Copyright © 2011-2022 走看看