<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=utf-8"><meta name=Generator content="Microsoft Word 14 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
p.MsoAcetate, li.MsoAcetate, div.MsoAcetate
        {mso-style-priority:99;
        mso-style-link:"Texte de bulles Car";
        margin:0in;
        margin-bottom:.0001pt;
        font-size:8.0pt;
        font-family:"Tahoma","sans-serif";}
span.TextedebullesCar
        {mso-style-name:"Texte de bulles Car";
        mso-style-priority:99;
        mso-style-link:"Texte de bulles";
        font-family:"Tahoma","sans-serif";}
span.EmailStyle19
        {mso-style-type:personal;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
span.EmailStyle20
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-US link=blue vlink=purple><div class=WordSection1><p class=MsoNormal><b><span lang=FR style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>De :</span></b><span lang=FR style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'> Gmail [mailto:mrjean1@gmail.com] <br><b>Envoyé :</b> Wednesday, September 24, 2014 2:41 PM<br><b>À :</b> Arulnambi Nandagoban<br><b>Objet :</b> Re: Python as Windows Service<o:p></o:p></span></p><p class=MsoNormal><o:p> </o:p></p><div><p class=MsoNormal>Also, this may work for you<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal><<a href="http://nssm.cc/description">http://nssm.cc/description</a>><o:p></o:p></p></div><div><p class=MsoNormal><<a href="http://nssm.cc/usage">http://nssm.cc/usage</a>><o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>NSSM installs an existing app as a service.<o:p></o:p></p></div><div><p class=MsoNormal><br><i>/Jean</i><o:p></o:p></p><div><p class=MsoNormal><o:p> </o:p></p></div></div><div><p class=MsoNormal style='margin-bottom:12.0pt'><br>On Sep 24, 2014, at 3:16 AM, "Arulnambi Nandagoban" <<a href="mailto:a.nandagoban@traxens.com">a.nandagoban@traxens.com</a>> wrote:<o:p></o:p></p></div><blockquote style='margin-top:5.0pt;margin-bottom:5.0pt'><div><p class=MsoNormal><b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>De :</span></b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'> MrJean1 [<a href="mailto:mrjean1@gmail.com">mailto:mrjean1@gmail.com</a>] <br><b>Envoyé :</b> Tuesday, September 23, 2014 5:55 PM<br><b>À :</b> <a href="mailto:a.nandagoban@traxens.com">a.nandagoban@traxens.com</a><br><b>Objet :</b> Python as Windows Service</span><o:p></o:p></p><p class=MsoNormal> <o:p></o:p></p><div><p class=MsoNormal>Here's one, simple example<o:p></o:p></p></div><div><p class=MsoNormal> <o:p></o:p></p></div><div><p class=MsoNormal><<a href="http://stackoverflow.com/questions/32404/is-it-possible-to-run-a-python-script-as-a-service-in-windows-if-possible-how">http://stackoverflow.com/questions/32404/is-it-possible-to-run-a-python-script-as-a-service-in-windows-if-possible-how</a>><br><br>There are more, just search for "Python Windows Service" for example.<o:p></o:p></p></div><div><p class=MsoNormal><br><i>/Jean</i><o:p></o:p></p><div><p class=MsoNormal><span style='color:#1F497D'> </span><o:p></o:p></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'> </span><o:p></o:p></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>Thank you !!! </span><o:p></o:p></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'> </span><o:p></o:p></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>--</span><o:p></o:p></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>Nambi</span><span style='font-size:11.0pt;font-family:"Calibri","sans-serif"'><o:p></o:p></span></p><div style='mso-element:para-border-div;border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in'><p class=MsoNormal style='border:none;padding:0in'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p></div><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>Thank you again !!!<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>I managed to run the server as as service with help of sample code provided in </span><<a href="http://stackoverflow.com/questions/32404/is-it-possible-to-run-a-python-script-as-a-service-in-windows-if-possible-how">http://stackoverflow.com/questions/32404/is-it-possible-to-run-a-python-script-as-a-service-in-windows-if-possible-how</a> .<o:p></o:p></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>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.<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>class TraxServerSvc (win32serviceutil.ServiceFramework):<o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>                _svc_name_ = "Testserver"<o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>                _svc_display_name_ = "Pytestserver"<o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>                def __init__(self,args):<o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>                                win32serviceutil.ServiceFramework.__init__(self,args)<o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>                                self.hWaitStop = win32event.CreateEvent(None,0,0,None)<o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>                                self.sloop = True<o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>                                self.serversock = 0<o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>                def SvcStop(self):<o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>                                self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)           <o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>                                win32event.SetEvent(self.hWaitStop)<o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>                                self.sloop = False<o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>                def SvcDoRun(self):<o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>                                servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE, servicemanager.PYS_SERVICE_STARTED, (self._svc_name_,''))<o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>                                self.main()<o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>                                <o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>                def main(self):<o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>                                self.serversock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>                                self.serversock.bind(server_config())<o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>                                self.serversock.listen(5)                               <o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>                                while (win32event.WaitForSingleObject(self.hWaitStop, 0) == 258):                                        <o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>                                                try:<o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>                                                                clientsock, addr = self.serversock.accept()<o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>                                                                Pysconfig.logger.info('...connected from: %s', addr) # logging<o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>                                                                thread.start_new_thread(handler, (clientsock, addr))<o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>                                                                Pysconfig.logger.info('Thread ID: %s', thread.get_ident()) # logging                                  <o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>                                                except socket.error, msg:<o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>                                                                Pysconfig.logger.info('Failed to create socket. Error code: ' + str(msg[0]) + ' , Error message : ' + msg[1])<o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>                                                                pass<o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>                                self.serversock.shutdown(socket.SHUT_RDWR)<o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>                                self.serversock.close()<o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>                                servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE, servicemanager.PYS_SERVICE_STOPPED, (self._svc_name_,''))<o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>if __name__ == '__main__':<o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>                win32serviceutil.HandleCommandLine(TraxServerSvc)<o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>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. <o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>--<o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:0in;margin-right:.5in;margin-bottom:0in;margin-left:.5in;margin-bottom:.0001pt'><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>nambi<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p></div></div></div></blockquote></div></body></html>