windows平台下如何用Python杀进程
来源:互联网 发布:html5网站模板源码 编辑:程序博客网 时间:2024/05/19 00:36
#!/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
0 0
- windows平台下如何用Python杀进程
- windows平台下如何用Python杀进程
- windows平台下如何用Python杀进程
- windows平台下如何用Python杀进程
- windows下,如何用C++编写守护进程
- 在windows下如何用c++编写守护进程
- 在windows下如何用c++编写守护进程
- windows下如何用python控制打印机打印
- Windows下如何用C语言打印当前进程的进程号?
- windows下如何用xmanager控制Linux?
- 【Windows技巧】如何用DOS命令关闭进程
- 技术分享:如何用Python和PyInstaller编写Windows恶意代码
- 如何用Python和PyInstaller编写Windows恶意代码
- 在windows XP下如何用Vmware装Linux操作系统~
- 在windows XP下如何用Vmware装Linux操作系统
- 在windows XP下如何用Vmware装Linux操作系统
- 在windows XP下如何用Vmware装Linux操作系统
- 在windows XP下如何用Vmware装Linux操作系统
- 毕业课题 ---opencv hog+svm bat批处理例子
- mongodb 在包含or条件查询的时候,尽量不使用排序
- py2exe笔记
- 分治算法 最接近点对(一维)
- python round(x)函数
- windows平台下如何用Python杀进程
- Python模块subprocess小记
- (转)如何使用Psyco为你的Python程序提速。
- python删除非空目录的方法
- Linux的cron和crontab
- sphinx启动searchd服务时,报segmentation fault (段错误)
- HDU_1175 连连看(BFS)
- (转载)谈CRC32碰撞的概率和可能性
- 万物互联,服务万家