Python as Windows Service

Arulnambi Nandagoban a.nandagoban at traxens.com
Wed Sep 24 16:49:21 CEST 2014


De : Gmail [mailto:mrjean1 at gmail.com] 
Envoyé : Wednesday, September 24, 2014 2:41 PM
À : Arulnambi Nandagoban
Objet : Re: Python as Windows Service

 

Also, this may work for you

 

<http://nssm.cc/description>

<http://nssm.cc/usage>

 

NSSM installs an existing app as a service.


/Jean

 


On Sep 24, 2014, at 3:16 AM, "Arulnambi Nandagoban" <a.nandagoban at traxens.com> wrote:

De : MrJean1 [mailto:mrjean1 at gmail.com] 
Envoyé : Tuesday, September 23, 2014 5:55 PM
À : a.nandagoban at traxens.com
Objet : Python as Windows Service

 

Here's one, simple example

 

<http://stackoverflow.com/questions/32404/is-it-possible-to-run-a-python-script-as-a-service-in-windows-if-possible-how>

There are more, just search for "Python Windows Service" for example.


/Jean

 

 

Thank you !!! 

 

--

Nambi

 

 

Thank you again !!!

 

I managed to run the server as as service with help of sample code provided in <http://stackoverflow.com/questions/32404/is-it-possible-to-run-a-python-script-as-a-service-in-windows-if-possible-how .

I found some problem in stopping the service.  I feel it because of socket.accept() which blocks. This the class where I implemented the server in the main method.

 

 

class TraxServerSvc (win32serviceutil.ServiceFramework):

                _svc_name_ = "Testserver"

                _svc_display_name_ = "Pytestserver"

 

                def __init__(self,args):

                                win32serviceutil.ServiceFramework.__init__(self,args)

                                self.hWaitStop = win32event.CreateEvent(None,0,0,None)

                                self.sloop = True

                                self.serversock = 0

 

                def SvcStop(self):

                                self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)           

                                win32event.SetEvent(self.hWaitStop)

                                self.sloop = False

 

                def SvcDoRun(self):

                                servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE, servicemanager.PYS_SERVICE_STARTED, (self._svc_name_,''))

                                self.main()

                                

                def main(self):

                                self.serversock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

                                self.serversock.bind(server_config())

                                self.serversock.listen(5)                               

                                while (win32event.WaitForSingleObject(self.hWaitStop, 0) == 258):                                        

                                                try:

                                                                clientsock, addr = self.serversock.accept()

                                                                Pysconfig.logger.info('...connected from: %s', addr) # logging

                                                                thread.start_new_thread(handler, (clientsock, addr))

                                                                Pysconfig.logger.info('Thread ID: %s', thread.get_ident()) # logging                                  

                                                except socket.error, msg:

                                                                Pysconfig.logger.info('Failed to create socket. Error code: ' + str(msg[0]) + ' , Error message : ' + msg[1])

                                                                pass

                                self.serversock.shutdown(socket.SHUT_RDWR)

                                self.serversock.close()

                                servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE, servicemanager.PYS_SERVICE_STOPPED, (self._svc_name_,''))

 

if __name__ == '__main__':

                win32serviceutil.HandleCommandLine(TraxServerSvc)

 

When win32event.WaitForSingleObject(self.hWaitStop, 0) method returns zero , I need to quit the while loop to stop the service. But in the above case, since it blocks in accept , I need to make connection from client to completely stop service. 

 

--

nambi

 

 

 

 

 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20140924/d728fc68/attachment.html>


More information about the Python-list mailing list