0
使用Windows服務控制檯,可以在「屬性」>「依賴項」下看到服務依賴項。你如何使用Python獲得相同的信息?有沒有辦法與psutil做到這一點?如何使用Python查看Windows服務依賴關係?
使用Windows服務控制檯,可以在「屬性」>「依賴項」下看到服務依賴項。你如何使用Python獲得相同的信息?有沒有辦法與psutil做到這一點?如何使用Python查看Windows服務依賴關係?
您可以使用subprocess
模塊查詢sc.exe
以獲取您的服務信息,然後解析出依賴關係信息。喜歡的東西:
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))
然後你就可以輕鬆地查詢依賴關係:
print(get_service_dependencies("wudfsvc")) # query Windows Driver Foundation service
# ['PlugPlay', 'WudfPf']