I had looked over the methods to list/kill processes on Win32, and could not (yet) find a "pure" Python way to do it that works on 9x, since I could not get win32pdhutil to work on 98.
So, I combined subprocess.py and pv.exe from
http://www.xmlsp.com/pview/PrcView.zip
and wrote a quick app to kill unauthorized process and compiled with py2exe.

Does anyone have a way to list/kill without the external process that works in all 9x and XP?


Ray Schumacher




#!/usr/bin/env python

import win32process, win32con
import time
import subprocess

whiteList = ["Eudora.exe", "Explorer.EXE", "MSTask.exe", "WCESCOMM.EXE",
    "WinMgmt.exe", "WnvIRQ32.exe", "cmd.exe",
    "explorer.exe", "firefox.exe", "hh.exe", "lsass.exe", "mdm.exe",
    "mplayer2.exe", "pv.exe", "python.exe", "pythonw.exe", 
    "services.exe", "smss.exe", "spoolsv.exe", "svchost.exe", "taskmgr.exe",
    "winlogon.exe"]

def launchWithoutConsole(command, args):
    """Launches 'command' windowless and waits until finished"""
    startupinfo = subprocess.STARTUPINFO()
    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
    proc = subprocess.Popen([command] + args, startupinfo=startupinfo,
            shell=True,
            bufsize=1000,
            stdin=subprocess.PIPE, stdout=subprocess.PIPE,
            )
    #print dir(proc)
    return proc.stdout.readlines()

def getProcList():
    pList = []
    result = launchWithoutConsole("pv\\pv", ["-fq",])
    result.sort()
    for i in result:
        pList.append((i.strip()).split('\t'))
    ## print the running processes
    #for i in pList:
    #    print '"'+i[0]+'",',
    return pList

def killProcess(proc):
    result = launchWithoutConsole("pv\\pv", ["-kfq", proc])
   
def main():
    while True:
        processList = getProcList()
        for i in processList:
            if i[0] not in whiteList:
                print i[0]+'is bad'
                killProcess(i[0])
        time.sleep(1)
   

if __name__ == '__main__':
    # Import Psyco if available
    try:
        import psyco
        #psyco.log()
        psyco.profile()
        #psyco.full()
    except ImportError:
        pass  
    main()
   
_______________________________________________
Python-win32 mailing list
Python-win32@python.org
http://mail.python.org/mailman/listinfo/python-win32

Reply via email to