windows平台下如何用Python杀进程
2015-06-19 16:16
726 查看
#!/usr/bin/env python
#coding=utf-8
'''Python语言: windows平台下如何用Python杀进程'''
#from : http://www.joyloft.net/?p=1031 # "这段代码来自于一个很简单的场景:
# 在python中调一个程序。给这个程序设置一个timeout
# 假如一段时间程序还没有返回,就杀掉这个这个新开的进程。
# 本来以为我肯定不是第一个遇到这个问题的人,
# 但是网上搜了好久都没找到完整的办法,自己搞了一个。"
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__':
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
killPid(pid)
print "End"
转载自:http://www.oschina.net/code/snippet_70229_2368
#coding=utf-8
'''Python语言: windows平台下如何用Python杀进程'''
#from : http://www.joyloft.net/?p=1031 # "这段代码来自于一个很简单的场景:
# 在python中调一个程序。给这个程序设置一个timeout
# 假如一段时间程序还没有返回,就杀掉这个这个新开的进程。
# 本来以为我肯定不是第一个遇到这个问题的人,
# 但是网上搜了好久都没找到完整的办法,自己搞了一个。"
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__':
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
killPid(pid)
print "End"
转载自:http://www.oschina.net/code/snippet_70229_2368
相关文章推荐
- python round(x)函数
- 用Python生成不依赖dll的exe的操作步骤简介
- python的xml库操作读写文件真是慢的无语,跟直接操作文本型的操作真不是一个级别的
- 使用Python和xml.etree.ElementTree解析xml文件
- python 调用word打开doc文档报错,“word无法启动转换器mswrd632.wpc”
- eclipse新建python项Project interpreter not specified
- python Word 编程
- python数组交集、并集与不同
- python数组交集、并集与不同
- python链接mysql常见问题汇总
- 【转】Python2.6下安装Mysqldb出现DeprecationWarning: the sets module is deprecated from sets错误解决方案
- python解码\'\\u\'字符串
- 用reindent.py修复python代码缩进(tab键引发的问题)
- python调用的动态连接库的方法(ctypes)
- Python urllib2 设置超时时间
- Python urllib2 设置超时时间
- 利用Python内置函数getattr轻松实现分发模式
- python获取文件属性(转)
- Python和Django的详细安装步骤(转)
- python和数据库连接(转)