[python-win32] list/kill processes on Win9x and NT+...

Ray Schumacher rays at blue-cove.com
Tue Jul 12 19:04:05 CEST 2005


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()
     
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-win32/attachments/20050712/e8d3e91d/attachment.htm


More information about the Python-win32 mailing list