Tuesday, April 27, 2010

Python: Service Class

Problem Statement:
Create a class for starting, stopping and getting status of a service.

Uses 2 methods:
- Windows Management Instrumentation
- SC Command from DOS

Solution:

import wmi
import os
import time
import string

class Services:
"""
1. The Service Class is aimed at starting and stopping the services based on the service name given as an input.
2. Class is instantiated with Service Name with the help of argumented constructor called __init__().
3. Function getstatus() gets the status of the services - Running or Stopped.
4. Functions start(),stop() would start and stop the services respectively by first getting the status of the service.
5. If the service is already started/stopped, it will print a message that the service is already running/stopped.
6. start() and stop() functions work in 2 modes: Using DOS command SC and using Windows Management Instrumentation. Default mode being "SC".
"""
def __init__(self, service):
self.wmiObj = wmi.WMI()
self.service = service
def getstatus(self):
return self.wmiObj.Win32_Service(Name=self.service)[0].State
def start(self, mode="sc"):
if mode.upper() == "SC":
try:
if(self.getstatus() == "Running"):
raise Exception("%s service is already running " % self.service)
else:
command = 'sc.exe start ' + self.service
os.system(command)
except Exception:
raise
if mode.upper() == "WMI":
try:
if self.getstatus()=="Running":
raise Exception("%s service is already running " % self.service)
else:
self.wmiObj.Win32_Service(Name=self.service)[0].StartService()
except Exception:
raise

def stop(self, mode="sc"):
if mode.upper() == "SC":
try:
if(self.getstatus() == "Stopped"):
raise Exception("%s service is already stopped " % self.service)
else:
command = 'sc.exe stop ' + self.service
os.system(command)
except Exception:
raise
if mode.upper() == "WMI":
try:
if self.getstatus()=="Stopped":
raise Exception("%s service is already stopped " % self.service)
else:
self.wmiObj.Win32_Service(Name=self.service)[0].StopService()
except Exception:
raise




No comments: