Dear James,

Sorry about that, I found a small error in the code, apparently I was
missing an "import pkg_resources"  in the code. 
once you install the service (WindowsService.py install) and start the
service (WindowsService.py start) you should find pythonservice.exe in
task manager.  The app starts pylons with the same setting that set up
the ini file, so with the default it should be running on port 5000. 
once you've started the service check your applicaiton log under the
event viewer, if it fails it'll show up there.  Please let me know if
that helps, I'd love to see something like this end up as part of pylons
Jose

James Gardner wrote:
> Hi Jose,
>
> An application like this would be extremely helpful and would be great 
> to include in Pylons with a bit of tweaking but I just can't quite get 
> it working on my machine. I can successfully update my INI file and then 
> install a service without errors but no application appears to actually 
> run when I start the installed service, there are no Python applications 
> in Task Manager for example. Which port does the application run on? Is 
> there a step I'm missing?
>
> Best wishes,
>
> James
>
>
> jose wrote:
>   
>> I've been playing around with pylons lately on windows and saw that
>> there
>> is no equivalent to demonize in paste for windows. So after looking
>> around at
>> lots of code I stole and modified some example windows service code
>> from cherrypy.  Basically if you put the windowservice.py fine in the
>> same file as your inifile, and run it it will add a new section to the
>> ini file which will allow you to define a windows service, running it
>> again a second time will allow you to install, and run the newly
>> defined service.  I'm including the file, hopefully someone else will
>> find it useful, and if anyone has suggestions on how to improve the
>> spaghetti code It would be much appreciated.
>>
>> Jose
>>
>>
>> === WindowsService.py ====
>>
>> """
>> The most basic (working) Windows service possible.
>> Requires Mark Hammond's pywin32 package.
>> Most of the code was taken from a  CherryPy 2.2 example of how to set
>> up a service
>> """
>>
>> import win32serviceutil
>> from paste.script.serve import ServeCommand as Server
>> import os, sys
>> import ConfigParser
>>
>> import win32service
>> import win32event
>>
>> class DefaultSettings(object):
>>     def __init__(self):
>>         os.chdir(os.path.dirname(__file__))
>>         # find the ini file
>>         self.ini = [x for x in os.listdir('.')
>>             if os.path.splitext(x)[1].lower().endswith('ini')]
>>         # create a config parser opject and populate it with the ini
>> file
>>         c = ConfigParser.SafeConfigParser()
>>         c.read(self.ini[0])
>>         self.c = c
>>
>>     def getDefaults(self):
>>         '''
>>         Check for and get the default settings
>>         '''
>>         if (
>>             (not self.c.has_section('winservice')) or
>>             (not self.c.has_option('winservice', 'service_name')) or
>>             (not self.c.has_option('winservice',
>> 'service_display_name')) or
>>             (not self.c.has_option('winservice',
>> 'service_description'))
>>             ):
>>             print 'setting defaults'
>>             self.setDefaults()
>>         service_name = self.c.get('winservice', 'service_name')
>>         service_display_name = self.c.get('winservice',
>> 'service_display_name')
>>         service_description = self.c.get('winservice',
>> 'service_description')
>>         iniFile = self.ini[0]
>>         return service_name, service_display_name, service_description,
>> iniFile
>>
>>     def setDefaults(self):
>>         '''
>>         set and add the default setting to the ini file
>>         '''
>>         if not self.c.has_section('winservice'):
>>             self.c.add_section('winservice')
>>         self.c.set('winservice', 'service_name', 'WSCGIService')
>>         self.c.set('winservice', 'service_display_name', 'WSCGI windows
>> service')
>>         self.c.set('winservice', 'service_description', 'WSCGI windows
>> service')
>>         cfg = file(self.ini[0], 'wr')
>>         self.c.write(cfg)
>>         cfg.close()
>>         print '''
>> you must set the winservice section service_name, service_display_name,
>> and service_description options to define the service
>> in the %s file
>> ''' % self.ini[0]
>>         sys.exit()
>>
>>
>> class MyService(win32serviceutil.ServiceFramework):
>>     """NT Service."""
>>
>>     d = DefaultSettings()
>>     service_name, service_display_name, service_description, iniFile =
>> d.getDefaults()
>>
>>     _svc_name_ = service_name
>>     _svc_display_name_ = service_display_name
>>     _svc_description_ = service_description
>>
>>     def __init__(self, args):
>>         win32serviceutil.ServiceFramework.__init__(self, args)
>>         # create an event that SvcDoRun can wait on and SvcStop
>>         # can set.
>>         self.stop_event = win32event.CreateEvent(None, 0, 0, None)
>>
>>     def SvcDoRun(self):
>>         os.chdir(os.path.dirname(__file__))
>>         s = Server(None)
>>         s.run([self.iniFile])
>>         win32event.WaitForSingleObject(self.stop_event,
>> win32event.INFINITE)
>>
>>     def SvcStop(self):
>>         self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
>>         #cherrypy.server.stop()
>>         #win32event.SetEvent(self.stop_event)
>>         self.ReportServiceStatus(win32service.SERVICE_STOPPED)
>>         sys.exit()
>>
>> if __name__ == '__main__':
>>     win32serviceutil.HandleCommandLine(MyService)
>>
>> ==========================================
>>
>>
>>     
>>   
>>     
>
>
> >
>
>   



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss
-~----------~----~----~----~------~----~------~--~---
"""
The most basic (working) Windows service possible.
Requires Mark Hammond's pywin32 package.  
Most of the code was taken from a  CherryPy 2.2 example of how to set up a 
service
"""
import pkg_resources
import win32serviceutil
from paste.script.serve import ServeCommand as Server
import os, sys
import ConfigParser

import win32service
import win32event

class DefaultSettings(object):
    def __init__(self):
        os.chdir(os.path.dirname(__file__))
        # find the ini file
        self.ini = [x for x in os.listdir('.') 
            if os.path.splitext(x)[1].lower().endswith('ini')]
        # create a config parser opject and populate it with the ini file
        c = ConfigParser.SafeConfigParser()
        c.read(self.ini[0])
        self.c = c
        
    def getDefaults(self):
        '''
        Check for and get the default settings
        '''
        if (
            (not self.c.has_section('winservice')) or
            (not self.c.has_option('winservice', 'service_name')) or
            (not self.c.has_option('winservice', 'service_display_name')) or
            (not self.c.has_option('winservice', 'service_description'))
            ):
            print 'setting defaults'
            self.setDefaults()
        service_name = self.c.get('winservice', 'service_name')
        service_display_name = self.c.get('winservice', 'service_display_name')
        service_description = self.c.get('winservice', 'service_description')
        iniFile = self.ini[0]
        return service_name, service_display_name, service_description, iniFile

    def setDefaults(self):
        '''
        set and add the default setting to the ini file
        '''
        if not self.c.has_section('winservice'):
            self.c.add_section('winservice')
        self.c.set('winservice', 'service_name', 'WSCGIService')
        self.c.set('winservice', 'service_display_name', 'WSCGI windows 
service')
        self.c.set('winservice', 'service_description', 'WSCGI windows service')
        cfg = file(self.ini[0], 'wr')
        self.c.write(cfg)
        cfg.close()
        print '''
you must set the winservice section service_name, service_display_name,
and service_description options to define the service 
in the %s file
''' % self.ini[0]
        sys.exit()


class MyService(win32serviceutil.ServiceFramework):
    """NT Service."""
    
    d = DefaultSettings()
    service_name, service_display_name, service_description, iniFile = 
d.getDefaults()
    
    _svc_name_ = service_name
    _svc_display_name_ = service_display_name
    _svc_description_ = service_description

    def __init__(self, args):
        win32serviceutil.ServiceFramework.__init__(self, args)
        # create an event that SvcDoRun can wait on and SvcStop
        # can set.
        self.stop_event = win32event.CreateEvent(None, 0, 0, None)

    def SvcDoRun(self):
        os.chdir(os.path.dirname(__file__))
        s = Server(None)
        s.run([self.iniFile])
        win32event.WaitForSingleObject(self.stop_event, win32event.INFINITE)
    
    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        #cherrypy.server.stop()
        #win32event.SetEvent(self.stop_event)
        self.ReportServiceStatus(win32service.SERVICE_STOPPED)
        sys.exit()

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

Reply via email to