python script under windows

cg notmuch at gmail.com
Tue Sep 20 10:52:23 EDT 2005


I ran into a similar issue a couple of months back, the solution on
Windows is to run it as a service. It is very simple, you need Mark
Hammond's Win32 extensions. For path you have to use absolute filepath
for all local files and for network drive use the UNC path i.e.
\\servername\folder-filename\ . All these steps will let your machine
running the program survive logouts after a login. If your machine is
part of windows network and there is domain login then in order for it
to work after a machine restart you need to goto the Service panel (in
Control Panel) find the Python service you registered, right-click and
goto its properties, goto the "Log On" panel, select a domain user for
"This account" by clicking the Browse button, note the selected user
has access to windows domain and admin access to that particular
machine. Enter user network password, hit Apply, OK and there u go. All
this requires admin access to machine. You can configure a couple of
things about the service in the Services panel.

The code itself is simple:
#------------------------------------------------------------------
import win32service, win32serviceutil

class MyService(win32serviceutil.ServiceFramework):
    """NT Service."""

    _svc_name_ = "MyServiceName"
    _svc_display_name_ = "A Little More Descriptive"

    def SvcDoRun(self):
        #do your stuff here, call your main application code.

    def SvcStop(self):
        #the following line is not really needed, basically put here
any code that should execute
        #before the service stops
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)

if __name__ == '__main__':
    win32serviceutil.HandleCommandLine(MyService)
#------------------------------------------------------------------
After this if you have python in your path and Win32 extensions
installed, goto command prompt and run:
c:\> MyService.py -startup=auto install

Trying to have your service have Network access after a machine restart
is a bit tricky. This thing works but somehow I feel there is more to
it. If anyone has a better way, please post.




More information about the Python-list mailing list