October 19, 2017, 09:28:49 AM
Welcome, Guest. Please login or register.

Author Topic: Running pyhton program as a service and having it install itself.  (Read 1442 times)

Offline Vector

  • Prospect
  • *
  • Posts: 37
  • Internets: +10/-24
    • @AntiSec_Inc
Alright so i am working on a python program and i have a module that will add a service for my program and then run itself. However i need my service to install itself and run at startup without having to install via the service handler. Here's my code.

Code: [Select]
import win32service
import win32serviceutil
import win32api
import win32con
import win32event
import win32evtlogutil
import os, sys, string, time


config_name = 'service.py'

# Find out where we are
if getattr(sys, 'frozen', False):
    application_path = os.path.dirname(sys.executable)
elif __file__:
    application_path = os.path.dirname(__file__)

config_path = os.path.join(application_path, config_name)

# Service
class aservice(win32serviceutil.ServiceFramework):

   _svc_name_ = "Generic Service"
   _svc_display_name_ = "Service Test"
   _svc_description_ = "Generic Description"

   def __init__(self, args):
      win32serviceutil.ServiceFramework.__init__(self, args)
      self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)         

   def SvcStop(self):
      self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
      win32event.SetEvent(self.hWaitStop)                   

   def SvcDoRun(self):
      import servicemanager     
      servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,servicemanager.PYS_SERVICE_STARTED,(self._svc_name_, ''))
      self.timeout = 1000
      # This is how long the service will wait to run / refresh itself

      while 1:
          # Wait for service stop signal, if we timeout, loop again
          rc = win32event.WaitForSingleObject(self.hWaitStop, self.timeout)
          # Check to see if self.hWaitStop happened
          if rc == win32event.WAIT_OBJECT_0:
              # Stop signal encountered
              servicemanager.LogInfoMsg("Generic Service - STOPPED!")  #For Event Log
              break
          else:
            # What to run
            try:
                file_path = config_path
                execfile(file_path)
            except:
                pass
            # End of what to run   

# Call service handler
def ctrlHandler(ctrlType):
   return True

if __name__ == '__main__': 
   win32api.SetConsoleCtrlHandler(ctrlHandler, True) 
   win32serviceutil.HandleCommandLine(aservice)

The relevant part here would be:

Code: [Select]
# Call service handler
def ctrlHandler(ctrlType):
   return True

if __name__ == '__main__': 
   win32api.SetConsoleCtrlHandler(ctrlHandler, True) 
   win32serviceutil.HandleCommandLine(aservice)

I need some logic that will let me replace that part with something that will have the service install and configure itself automatically without user input. Thoughts?
"Words have no power to impress the mind without the exquisite horror of their reality"

Offline null_void

  • Top Hat Member
  • Enthusiast
  • ********
  • Posts: 51
  • Internets: +2/-0
Re: Running pyhton program as a service and having it install itself.
« Reply #1 on: August 24, 2015, 03:41:23 PM »
FERNSEC just for clarification what is the Operation system? (2000, XP, 7,8 or 10?)It looks like Windows based on the code.  Just asking to get my head around the code.
The most powerful weapon you have is knowledge! They may take your life but the can never take what is inside your mind.