+ -
当前位置:首页 → 问答吧 → 请教下python 获取系统服务状态的方法

请教下python 获取系统服务状态的方法

时间:2011-10-20

来源:互联网

请教下python 获取系统服务状态的方法?

作者: mouseweiwei   发布时间: 2011-10-20

Python code

def listservices():
    import wmi
    
    c = wmi.WMI()
    for service in c.Win32_Service():
        print service.Caption,service.StartMode,service.State
        
if __name__=='__main__':
    listservices()



运行输出:
D:\test\epp>c:\python27\python 20111020a.py
Application Experience Manual Stopped
Application Layer Gateway Service Manual Stopped
Application Identity Manual Stopped
Application Information Manual Stopped
Apple Mobile Device Manual Stopped
Application Management Manual Stopped
ASP.NET State Service Manual Stopped
Windows Audio Endpoint Builder Auto Running
Windows Audio Auto Running
......



需要安装wmi模块,这个模块又需要安装pywin模块。

作者: panzifei   发布时间: 2011-10-20

360主动防御服务有中文,如果你保存为utf8格式,这样一下显示中文服务名出来:
print service.Caption.encode('gbk'),service.StartMode,service.State

作者: panzifei   发布时间: 2011-10-20

感谢 帮助 我试试你这个方法

我刚才自己想了个很呆的方法
os.system("sc query Messanger |findstr RUNNING")
用DOS 去判断某些服务是不是开了。。
感觉这么做很不正统
对PY的类库很不熟悉 额

作者: mouseweiwei   发布时间: 2011-10-20