Validate service

-1

I'm having difficulty returning values from the 'psutil' library, I need to create a script that finds a service and returns me whether it is running or not, however the service name changes according to the example user's machine : Machine 01 has the service: SRV $ Lokj Machine 02 has the service: SRV $ Kujh

And so on ...

My difficulty is just to filter the values, I tried to use the command servico.find ('SRV $ *') but the return was this:

AttributeError: 'list' object has no attribute 'find'

Code:

import psutil
x = 0
s = list(psutil.win_service_iter())
print (s.find('SRV$*'))
    
asked by anonymous 06.08.2018 / 16:32

1 answer

0

I never moved with the psutil library, but answering the question of how to find items in the list that have a string: A list does not really have this find () function. I thought of doing a function that gets a list and a string to look for:

def procura(lista, string_procurada):
    itens_encontrados = []
    for item in lista:
        if string_procurada in item:
            itens_encontrados.append(item)
    return itens_encontrados

Example usage:

uma_lista = ['SRV$service1','bla','SRV$service2','blabla','SRV$service3']
resultado = find(uma_lista, 'SRV$')
>>> print (resultado)
['SRV$service1', 'SRV$service2', 'SRV$service3']

Note: If you want only the values that start with 'SRV $', then we have to modify the function for something like:

def procura(lista, string_procurada):
    itens_encontrados = []
    for item in lista:
        if item[0:len(string_procurada)] == string_procurada:
            itens_encontrados.append(item)
    return itens_encontrados
    
06.08.2018 / 18:00