如何使用Python查看Windows服务依赖项? [英] How to see a Windows service dependencies with Python?
本文介绍了如何使用Python查看Windows服务依赖项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用Windows服务控制台,您可以在属性">依赖关系"下看到服务依赖关系.如何使用Python获得相同的信息? psutil有办法吗?
Using the Windows Services console, you can see a service dependencies under Properties > Dependencies. How you can you get the same information with Python? Is there a way to do it with psutil?
推荐答案
您可以使用subprocess
模块查询sc.exe
以获得服务信息,然后解析出依赖项信息.像这样:
You can use the subprocess
module to query sc.exe
to get info for your service and then parse out the dependencies information. Something like:
import subprocess
def get_service_dependencies(service):
try:
dependencies = [] # hold our dependency list
info = subprocess.check_output(["sc", "qc", service], universal_newlines=True)
dep_index = info.find("DEPENDENCIES") # find the DEPENDENCIES entry
if dep_index != -1: # make sure we have a dependencies entry
for line in info[dep_index+12:].split("\n"): # loop over the remaining lines
entry, value = line.rsplit(":", 2) # split each line to entry : value
if entry.strip(): # next entry encountered, no more dependencies
break # nothing more to do...
value = value.strip() # remove the whitespace
if value: # if there is a value...
dependencies.append(value) # add it to the dependencies list
return dependencies or None # return None if there are no dependencies
except subprocess.CalledProcessError: # sc couldn't query this service
raise ValueError("No such service ({})".format(service))
然后,您可以轻松查询依赖项,如下所示:
Then you can easily query for dependencies as:
print(get_service_dependencies("wudfsvc")) # query Windows Driver Foundation service
# ['PlugPlay', 'WudfPf']
这篇关于如何使用Python查看Windows服务依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文