如何查看python的依赖_如何使用Python查看Windows服务依赖项?

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?

解决方案

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']

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值