Building a Windows NT service with py2exe

Adam Albrechtas adam.albrechtas at equisure.ca
Wed Mar 26 11:32:40 EST 2003


I have coded a Windows NT service in Python 2.2.  The service works
when running as a python script.  I can use py2exe to roll it into an
.exe without specifying that it is a service (which means that the
.exe ultimately does not work but at least the process completes). 
When I use py2exe while specifying a service build I receive the
following:

<snip -- no errors or warnings above this point>
changing into 'build\bdist.win32\winexe\collect\NTAdminService'
zip -rq C:\work\python2\ntadmin.py2exe\build\bdist.win32\winexe\NTAdminService.z
ip .
creating 'C:\work\python2\ntadmin.py2exe\build\bdist.win32\winexe\NTAdminService
.zip' and adding '.' to it
changing back to 'C:\work\python2\ntadmin.py2exe'
creating dist\NTAdminService\NTAdminService.exe
Traceback (most recent call last):
  File "C:\work\python2\ntadmin.py2exe\setup.py", line 4, in ?
    setup(name='NTAdminService',scripts=['NTAdminService.py'])
  File "C:\Python22\lib\distutils\core.py", line 138, in setup
    dist.run_commands()
  File "C:\Python22\lib\distutils\dist.py", line 893, in run_commands
    self.run_command(cmd)
  File "C:\Python22\lib\distutils\dist.py", line 913, in run_command
    cmd_obj.run()
  File "C:\Python22\Lib\site-packages\py2exe\build_exe.py", line 626,
in run
    self.create_exe(exe_name, arcname, use_runw)
  File "C:\Python22\Lib\site-packages\py2exe\build_exe.py", line 823,
in create_
exe
    svc_name, svc_display_name, svc_deps = self.get_service_names()
  File "C:\Python22\Lib\site-packages\py2exe\build_exe.py", line 898,
in get_ser
vice_names
    exec compile(open(self.script).read(), self.script, "exec") in
mod.__dict__
  File "<string>", line 45
    win32serviceutil.HandleCommandLine(NTAdminService)
                                                    ^
SyntaxError: invalid syntax


I have read that py2exe does not always work for every script.  I am
thinking this may be the case for me (but I am hoping not).  Help me
end the pain!

Adam Albrechtas

<Additional Information>
File: setup.py
---------------
from distutils.core import setup
import py2exe

setup(name='NTAdminService',scripts=['NTAdminService.py'])

File: setup.cfg
----------------
[py2exe]
service=NTAdminService

File: NTAdminService.py
------------------------
import win32serviceutil
import win32service
import win32event
import win32api
import win32evtlogutil
import win32file
import sys,SocketServer,socket
import NTAdminServerHandler

class NTAdminService(win32serviceutil.ServiceFramework):
    _svc_name_ = "NTAdminService"
    _svc_display_name_ = "NT Admin Tool"
    __SERVER_PORT = 31310
    
    def __init__(self, args):
        win32evtlogutil.AddSourceToRegistry(self._svc_display_name_,sys.executable,"Application")
        win32serviceutil.ServiceFramework.__init__(self, args)
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
        self.hSockEvent = win32event.CreateEvent(None, 0, 0, None)

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

    def SvcDoRun(self):
        import servicemanager
        # Write a 'started' event to the event log...
        win32evtlogutil.ReportEvent(self._svc_display_name_,servicemanager.PYS_SERVICE_STARTED,0,servicemanager.EVENTLOG_INFORMATION_TYPE,(self._svc_name_,
''))

        server = SocketServer.TCPServer(('',self.__SERVER_PORT),NTAdminServerHandler.NTAdminRequestHandler)

        while 1:
            win32file.WSAEventSelect(server.socket, self.hSockEvent,
win32file.FD_ACCEPT)
            
            rc = win32event.WaitForMultipleObjects((self.hWaitStop,
self.hSockEvent), 0, win32event.INFINITE)
            if rc == win32event.WAIT_OBJECT_0:
                break
            else:
                win32file.WSAEventSelect(server.socket,
self.hSockEvent, 0)
                server.socket.setblocking(1)
                server.handle_request()

        # and write a 'stopped' event to the event log.
        win32evtlogutil.ReportEvent(self._svc_display_name_,servicemanager.PYS_SERVICE_STOPPED,0,servicemanager.EVENTLOG_INFORMATION_TYPE,(self._svc_name_,
""))

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




More information about the Python-list mailing list