前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >Python监视进程创建情况和系统服务状态

Python监视进程创建情况和系统服务状态

作者头像
Python小屋屋主
发布2018-04-16 14:11:57
发布2018-04-16 14:11:57
1.1K0
举报
文章被收录于专栏:Python小屋Python小屋

(1)监视Windows系统中进程创建情况

import wmi

c = wmi.WMI()

process_watcher = c.Win32_Process.watch_for('creation')

while True:

try:

new_process = process_watcher()

proc_owner = '{0[0]}\\{0[1]}'.format(new_process.GetOwner())

temp_creation_date = new_process.CreationDate

creation_date = temp_creation_date[:4]

for i in (4, 6):

creation_date += '-'+temp_creation_date[i:i+2]

creation_date += ' '

for i in (8, 10, 12):

creation_date += temp_creation_date[i:i+2]+':'

creation_date = creation_date[:-1]

executable = new_process.ExecutablePath

cmdline = new_process.CommandLine

pid = new_process.ProcessId

parent_pid = new_process.ParentProcessId

print('='*30)

print('Process owner:'.ljust(18), proc_owner)

print('Creation Time:'.ljust(18), str(creation_date))

print('Executable:'.ljust(18), executable)

print('Cmdline:'.ljust(18), cmdline)

print('ProcessId:'.ljust(18), pid)

print('Parent ProcessId:'.ljust(18), parent_pid)

except:

pass (2)查看Windows系统中服务状态 import itertools

import wmi

def group(service):

if service.State == 'Stopped':

return 'Stopped'

elif service.State == 'Running':

return 'Running'

else:

return 'Others'

result = dict()

c = wmi.WMI()

for service in c.Win32_Service():

state = service.State

caption = service.Caption

t = result.get(state,[])

t.append(caption)

result[state] = t

for state, captions in result.items():

print('='*30)

print(state)

print('\n'.join(sorted(captions)))

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2016-07-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python小屋 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档