Monday, August 21, 2017

skeleton for a simple service of windows 7

*** service.py ***
import win32serviceutil
import win32service
import win32event
import servicemanager
import socket

import xmlrpclib
from SimpleXMLRPCServer import SimpleXMLRPCServer
import os




class AppServerSvc (win32serviceutil.ServiceFramework):
    _svc_name_ = "TestService"
    _svc_display_name_ = "Test Service"

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

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

    def SvcDoRun(self):
        servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                              servicemanager.PYS_SERVICE_STARTED,
                              (self._svc_name_,''))
        self.main()
    # def is_even(n):
    #     return n % 2 == 0

    def pinghost(self,ip):
        os.system("ping -n 1 " + ip)
        return "good"

    # def main(self):
    #     pass

    def main(self):
        server = SimpleXMLRPCServer(("0.0.0.0", 8000))
        print "Listening on port 8000..."
        # server.register_function(is_even, "is_even")
        server.register_function(self.pinghost, "pinghost")
        server.serve_forever()

if __name__ == '__main__':
    win32serviceutil.HandleCommandLine(AppServerSvc)





**************
- need pywin32 lib


1- install server
python service.py install

2- to remove it
python service.py remove

No comments:

Post a Comment