玩蛇网提供最新Python编程技术信息以及Python资源下载!

用Python杀进程的方法之windows平台示例详解

python 培训

本篇Python语言源码为:如何在windows平台下如何用Python杀进程的方法。主要的思路是在Python程序设置一个timeout,条件是一定时间内,如果这个程序没有返回,那么执行“杀掉这个这个新开的进程”。这就是我大概的思路,大家也可以自由发挥用你们的方法来杀进程。

python windows杀进程方法

如下就是我用Python杀进程的方法(windows)源码:需要用到ctypes、sys两个方法模块

import ctypes
import sys

TH32CS_SNAPPROCESS = 0x00000002

class PROCESSENTRY32(ctypes.Structure):
     _fields_ = [("dwSize", ctypes.c_ulong),
                 ("cntUsage", ctypes.c_ulong),
                 ("th32ProcessID", ctypes.c_ulong),
                 ("th32DefaultHeapID", ctypes.c_ulong),
                 ("th32ModuleID", ctypes.c_ulong),
                 ("cntThreads", ctypes.c_ulong),
                 ("th32ParentProcessID", ctypes.c_ulong),
                 ("pcPriClassBase", ctypes.c_ulong),
                 ("dwFlags", ctypes.c_ulong),
                 ("szExeFile", ctypes.c_char * 260)]

def getProcList():
    CreateToolhelp32Snapshot = ctypes.windll.kernel32.CreateToolhelp32Snapshot
    Process32First = ctypes.windll.kernel32.Process32First
    Process32Next = ctypes.windll.kernel32.Process32Next
    CloseHandle = ctypes.windll.kernel32.CloseHandle
    hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
    pe32 = PROCESSENTRY32()
    pe32.dwSize = ctypes.sizeof(PROCESSENTRY32)

    if Process32First(hProcessSnap,ctypes.byref(pe32)) == False:
        print >> sys.stderr, "Failed getting first process."
        return

    while True:
        yield pe32
        if Process32Next(hProcessSnap,ctypes.byref(pe32)) == False:
            break
    CloseHandle(hProcessSnap)

def getChildPid(pid):

    procList = getProcList()

    for proc in procList:

        if proc.th32ParentProcessID == pid:
            yield proc.th32ProcessID
    
def killPid(pid):

    childList = getChildPid(pid)

    for childPid in childList:
        killPid(childPid)

    handle = ctypes.windll.kernel32.OpenProcess(1, False, pid)

    ctypes.windll.kernel32.TerminateProcess(handle,0)


if __name__ =='__main__':

    args = sys.argv 

    if len(args) >1 :
        pid = int(args[1])
        killPid(pid)

    else:
        procList = getProcList()
        for proc in procList:
            print proc.szExeFile+'  '+str(proc.th32ParentProcessID) + '  '+str(proc.th32ProcessID)
    

#----------------------
#
# www.iplaypython.com
#
#----------------------

import subprocess
import time

#import winproc

timeout = 2

process = subprocess.Popen("cmd /k ping localhost -t",shell = True)

start = int(time.time())

while process.poll()==None:

    now = int(time.time())

    if int (now - start) >timeout:
        pid = process.pid
        break

winproc.killPid(pid)
        
print "End"

网上还有好多关于 用Python语言杀进程的方法,但都不是太完整。希望我的这个源码能帮到有这方面需求的朋友们。

玩蛇网原创,转载请注明文章出处和来源网址:http://www.iplaypython.com/code/base/b2620.html



微信公众号搜索"玩蛇网Python之家"加关注,每日最新的Python资讯、图文视频教程可以让你一手全掌握。强烈推荐关注!

微信扫描下图可直接关注

玩蛇网Python新手QQ群,欢迎加入: ① 240764603 玩蛇网Python新手群
文章发布日期:2016-01-04 21:13 玩蛇网 www.iplaypython.com

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
相关文章推荐
别人正在看
特别推荐
去顶部去底部