win32 service
Hi everybody, have a little problem with a service on Win32. I use a TCP server as service, but can't access from an other machine. Only local access is possible. The service starts like this: -> myService.py --username user --password password install <- followed by start The user is member in "Log on as service", but... only local access :-( What is wrong or what can I do ? gerd -- http://mail.python.org/mailman/listinfo/python-list
Starting Win32 Service
Hi all, Using Tim Golden's wmi module you can get the service names import wmi c = wmi.WMI () stopped_services = c.Win32_Service (StartMode="Auto", State="Stopped") if stopped_services: for s in stopped_services: print s.Caption, "service is not running" else: print "No auto services stopped" http://tgolden.sc.sabren.com/python/wmi_cookbook.html#automatic_services but how do i start services that are stopped? Cheers -- http://mail.python.org/mailman/listinfo/python-list
Re: win32 service
On 16/12/06, g.franzkowiak <[EMAIL PROTECTED]> wrote: > Hi everybody, > > have a little problem with a service on Win32. > > I use a TCP server as service, but can't access from an other machine. > Only local access is possible. > > The service starts like this: > > -> myService.py --username user --password password install <- > > followed by start > > The user is member in "Log on as service", but... only local access :-( > > What is wrong or what can I do ? > Have you checked that your firewall isn't causing the problem, and that the servers IP address is accessible from other machines to start with ? -- http://mail.python.org/mailman/listinfo/python-list
Re: win32 service
Tim Williams schrieb: > On 16/12/06, g.franzkowiak <[EMAIL PROTECTED]> wrote: >> Hi everybody, >> >> have a little problem with a service on Win32. >> >> I use a TCP server as service, but can't access from an other machine. >> Only local access is possible. >> >> The service starts like this: >> >> -> myService.py --username user --password password install <- >> >> followed by start >> >> The user is member in "Log on as service", but... only local access :-( >> >> What is wrong or what can I do ? >> > > Have you checked that your firewall isn't causing the problem, and > that the servers IP address is accessible from other machines to start > with ? Yes, is checked. The server class operates as normal console program. -- http://mail.python.org/mailman/listinfo/python-list
Re: win32 service
Tim Williams schrieb: > On 16/12/06, g.franzkowiak <[EMAIL PROTECTED]> wrote: >> Hi everybody, >> >> have a little problem with a service on Win32. >> >> I use a TCP server as service, but can't access from an other machine. >> Only local access is possible. >> >> The service starts like this: >> >> -> myService.py --username user --password password install <- >> >> followed by start >> >> The user is member in "Log on as service", but... only local access :-( >> >> What is wrong or what can I do ? >> > > Have you checked that your firewall isn't causing the problem, and > that the servers IP address is accessible from other machines to start > with ? OK, now I'm connected. My adjustments in "Log on as service" were wrong. gerd -- http://mail.python.org/mailman/listinfo/python-list
win32 service and sockets
Hi, I created a win32 service for XPPro called N4010ATestService.py (see below). The service runs as a particular user with administrative rights. It starts a thread that creates a simple socket server (N4010ASocketServer.py -- also below) that just waits for 20 character string. When I run the socket server by itself the test client can connect to the server and send a 20 character string just fine. When I run the service, the server will bind to the port but the client cannot connect. I verified the server was listening on the given port using netstat -an. The client eventually times out. Why isn't the server accepting connections when run in a service? Thanks, Tom N4010ATestService.py: - import win32serviceutil import win32service import win32event import N4010ASocketServer from thread import start_new_thread class N4010ATestService(win32serviceutil.ServiceFramework): _svc_name_ = "N4010ATestService" _svc_display_name_ = "N4010A Test Service" def __init__(self, args): win32serviceutil.ServiceFramework.__init__(self, args) def SvcDoRun(self): start_new_thread(N4010ASocketServer.main, ()) N4010ASocketServer.waitfor() def SvcStop(self): # Before we do anything, tell the SCM we are starting the stop process. self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) N4010ASocketServer.stop() if __name__ == '__main__': win32serviceutil.HandleCommandLine(N4010ATestService) N4010ASocketServer.py: -- from socket import socket from select import select from time import sleep import win32evtlogutil applicationName = 'N4010ASocketServer' messageDll = 'C:\Python24\Lib\site-packages\win32\pythonservice.exe' running = True stopped = False def registerWithEventViewer(): win32evtlogutil.AddSourceToRegistry(applicationName, messageDll) def stop(): import servicemanager servicemanager.LogInfoMsg('stopping') global running running = False def waitfor(): while not stopped: sleep(0.5) def main(): import servicemanager registerWithEventViewer() servicemanager.LogInfoMsg('creating socket') sock = socket() servicemanager.LogInfoMsg('binding to port 48777') sock.bind(('0.0.0.0', 48777)) servicemanager.LogInfoMsg('listening') sock.listen(5) while running: servicemanager.LogInfoMsg('Waiting for connection.') readyRead, readyWrite, inerror = select([sock], [], [], 0) while (not readyRead) and running: sleep(0.5) readyRead, readyWrite, inerror = select([sock], [], [], 0) if running: conn, address = sock.accept() msg = conn.recv(20) servicemanager.LogInfoMsg('Recvd msg: %s' % msg) conn.close() global stopped stopped = True -- http://mail.python.org/mailman/listinfo/python-list
win32 service and time.sleep()
I have a win32 service written in Python. It works well. It sends a report of the status of the machine via email periodically. The one problem I have is this... while trying to send an email, the script loops until a send happens and then it breaks. Should it be unable to send, it sleeps for 10 minutes with time.sleep(600) and then wakes and tries again. This is when the problem occurs. I can't stop the service while the program is sleeping. When I try, it just hangs until a reboot. Can some suggest how to fix this? Thanks, rbt -- http://mail.python.org/mailman/listinfo/python-list
win32 service and time.sleep()
I have a win32 service written in Python. It works well. It sends a report of the status of the machine via email periodically. The one problem I have is this... while trying to send an email, the script loops until a send happens and then it breaks. Should it be unable to send, it sleeps for 10 minutes with time.sleep(600) and then wakes and tries again. This is when the problem occurs. I can't stop the service while the program is sleeping. When I try, it just hangs until a reboot. Can some suggest how to fix this? Thanks, rbt -- http://mail.python.org/mailman/listinfo/python-list
RE: Starting Win32 Service
[placid] | Using Tim Golden's wmi module you can get the service names | | import wmi | c = wmi.WMI () | stopped_services = c.Win32_Service (StartMode="Auto", State="Stopped") | if stopped_services: | for s in stopped_services: | print s.Caption, "service is not running" | else: | print "No auto services stopped" | | but how do i start services that are stopped? import wmi c = wmi.WMI () for method in c.Win32_Service._methods: print method # #... includes StartService # print c.Win32_Service.StartService # (ReturnValue)> # # Therefore, to start all non-running auto services (!) # for service in c.Win32_Service (StartMode="Auto", State="Stopped"): print service.Caption service.StartService () TJG This e-mail has been scanned for all viruses by Star. The service is powered by MessageLabs. For more information on a proactive anti-virus service working around the clock, around the globe, visit: http://www.star.net.uk -- http://mail.python.org/mailman/listinfo/python-list
Re: Starting Win32 Service
At Wednesday 27/9/2006 01:39, placid wrote: Using Tim Golden's wmi module you can get the service names but how do i start services that are stopped? Surely there are fancier ways: >net start servicename Gabriel Genellina Softlab SRL __ Preguntá. Respondé. Descubrí. Todo lo que querías saber, y lo que ni imaginabas, está en Yahoo! Respuestas (Beta). ¡Probalo ya! http://www.yahoo.com.ar/respuestas -- http://mail.python.org/mailman/listinfo/python-list
Re: Starting Win32 Service
Tim Golden wrote: > [placid] > | Using Tim Golden's wmi module you can get the service names > | > | import wmi > | c = wmi.WMI () > | stopped_services = c.Win32_Service (StartMode="Auto", State="Stopped") > | if stopped_services: > | for s in stopped_services: > | print s.Caption, "service is not running" > | else: > | print "No auto services stopped" > | > | but how do i start services that are stopped? > > > import wmi > c = wmi.WMI () > for method in c.Win32_Service._methods: > print method > > # > #... includes StartService > # > > print c.Win32_Service.StartService > > # (ReturnValue)> > > # > # Therefore, to start all non-running auto services (!) > # > for service in c.Win32_Service (StartMode="Auto", State="Stopped"): > print service.Caption > service.StartService () > > > > TJG > Thanks for that. Now i was trying to use service.ChangeStartMode but each time i pass in an argument i get the error; #TypeError: __call__() takes exactly 1 argument (2 given) but; >>> print service.ChangeStartMode (ReturnValue)> ChangeStartMode needs an argument! What im i doing wrong here? Cheers -- http://mail.python.org/mailman/listinfo/python-list
Re: Starting Win32 Service
placid wrote: > Tim Golden wrote: > > [placid] > > | Using Tim Golden's wmi module you can get the service names > > | > > | import wmi > > | c = wmi.WMI () > > | stopped_services = c.Win32_Service (StartMode="Auto", State="Stopped") > > | if stopped_services: > > | for s in stopped_services: > > | print s.Caption, "service is not running" > > | else: > > | print "No auto services stopped" > > | > > | but how do i start services that are stopped? > > > > > > import wmi > > c = wmi.WMI () > > for method in c.Win32_Service._methods: > > print method > > > > # > > #... includes StartService > > # > > > > print c.Win32_Service.StartService > > > > # (ReturnValue)> > > > > # > > # Therefore, to start all non-running auto services (!) > > # > > for service in c.Win32_Service (StartMode="Auto", State="Stopped"): > > print service.Caption > > service.StartService () > > > > > > > > TJG > > > > Thanks for that. > > Now i was trying to use service.ChangeStartMode but each time i pass in > an argument i get the error; > > #TypeError: __call__() takes exactly 1 argument (2 given) > > but; > > >>> print service.ChangeStartMode > (ReturnValue)> > > ChangeStartMode needs an argument! > > What im i doing wrong here? And to answer my own question, the way to pass in arguments is via the key=value way so; service.ChangeStartMode(StartMode = "Automatic") Cheers P.S: Dont you just love trial and error/brute force? -- http://mail.python.org/mailman/listinfo/python-list
RE: Starting Win32 Service
[placid] | > Now i was trying to use service.ChangeStartMode but each | time i pass in | > an argument i get the error; | > | > #TypeError: __call__() takes exactly 1 argument (2 given) | > | > but; | > | > >>> print service.ChangeStartMode | > (ReturnValue)> | > | > ChangeStartMode needs an argument! | > | > What im i doing wrong here? | | And to answer my own question, the way to pass in arguments is via the | key=value way so; | | service.ChangeStartMode(StartMode = "Automatic") Yes, sorry about that, it's a well-known (to me) gotcha. Basically there's no way I can extract the params from the COM interface in a way which implies order, so I can't take them in positionally. (Corrections to this statement will be gratefully received). TJG This e-mail has been scanned for all viruses by Star. The service is powered by MessageLabs. For more information on a proactive anti-virus service working around the clock, around the globe, visit: http://www.star.net.uk -- http://mail.python.org/mailman/listinfo/python-list
Re: Starting Win32 Service
Tim Golden wrote: ... > Yes, sorry about that, it's a well-known (to me) gotcha. > Basically there's no way I can extract the params from > the COM interface in a way which implies order, so I > can't take them in positionally. (Corrections to this > statement will be gratefully received). > > TJG InParameters.Properties_ can be accessed by name or index, so you should be able to map both *args and **kwargs. (although you'd have to check for overlap yourself) Roger == Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News== http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups = East and West-Coast Server Farms - Total Privacy via Encryption = -- http://mail.python.org/mailman/listinfo/python-list
RE: Starting Win32 Service
| Tim Golden wrote: | ... | > Yes, sorry about that, it's a well-known (to me) gotcha. | > Basically there's no way I can extract the params from | > the COM interface in a way which implies order, so I | > can't take them in positionally. (Corrections to this | > statement will be gratefully received). | > | > TJG | | InParameters.Properties_ can be accessed by name or index, | so you should be able to map both *args and **kwargs. | (although you'd have to check for overlap yourself) | | Roger Thanks a lot, Roger. I thought I'd looked around this one before, but obviously not hard enough! I'll have another look. TJG This e-mail has been scanned for all viruses by Star. The service is powered by MessageLabs. For more information on a proactive anti-virus service working around the clock, around the globe, visit: http://www.star.net.uk -- http://mail.python.org/mailman/listinfo/python-list
Re: win32 service and sockets
On Tuesday 08 February 2005 16:41, Tom Brown wrote: > Hi, > > I created a win32 service for XPPro called N4010ATestService.py (see > below). The service runs as a particular user with administrative rights. > It starts a thread that creates a simple socket server > (N4010ASocketServer.py -- also below) that just waits for 20 character > string. When I run the socket server by itself the test client can connect > to the server and send a 20 character string just fine. When I run the > service, the server will bind to the port but the client cannot connect. I > verified the server was listening on the given port using netstat -an. The > client eventually times out. Why isn't the server accepting connections > when run in a service? > > Thanks, > Tom > Well, I have found that it works if I launch the client on the same machine as the service. It will not work from a remote machine. Any ideas? Thanks, Tom -- http://mail.python.org/mailman/listinfo/python-list
Re: win32 service and sockets
Tom Brown <[EMAIL PROTECTED]> writes: > Well, I have found that it works if I launch the client on the same > machine as the service. It will not work from a remote machine. Any > ideas? Sounds like it might be an issue at the network layer rather than in your code - perhaps a routing or filtering problem between your two machines. Have you verified that you do in fact have network connectivity between the machines (such as with ping), and that you can reach your server's port from the client (perhaps try telnetting to the port). Since you mentioned Xp, could any of it's built-in firewall support be enabled, and perhaps blocking access to your server's port? -- David -- http://mail.python.org/mailman/listinfo/python-list
Re: win32 service and sockets
On Wednesday 09 February 2005 10:48, David Bolen wrote: > Tom Brown <[EMAIL PROTECTED]> writes: > > Well, I have found that it works if I launch the client on the same > > machine as the service. It will not work from a remote machine. Any > > ideas? > > Since you mentioned Xp, could any of it's built-in firewall support be > enabled, and perhaps blocking access to your server's port? > David, That was it. It was the built-in firewall that was getting me. Thanks! Tom -- http://mail.python.org/mailman/listinfo/python-list
Re: win32 service and time.sleep()
On Tue, 20 Sep 2005 10:49:13 -0400, rbt wrote: > I have a win32 service written in Python. It works well. It sends a > report of the status of the machine via email periodically. The one > problem I have is this... while trying to send an email, the script > loops until a send happens and then it breaks. Should it be unable to > send, it sleeps for 10 minutes with time.sleep(600) and then wakes and > tries again. This is when the problem occurs. I can't stop the service > while the program is sleeping. When I try, it just hangs until a reboot. > Can some suggest how to fix this? > You could try doing it the hard way. In a loop, sleep for 1-5 seconds at a time. Everytime you complete a sleep, check the elapsed time. If the elapsed time is >= the total sleep duration, fall out of the sleep loop and try your email again. This should allow your service to stop within 1-5 seconds of your request while imposing little to no load on the system. -- http://mail.python.org/mailman/listinfo/python-list
Re: win32 service and time.sleep()
rbt wrote: >I have a win32 service written in Python. It works well. It sends a >report of the status of the machine via email periodically. The one >problem I have is this... while trying to send an email, the script >loops until a send happens and then it breaks. Should it be unable to >send, it sleeps for 10 minutes with time.sleep(600) and then wakes and >tries again. This is when the problem occurs. I can't stop the service >while the program is sleeping. When I try, it just hangs until a reboot. >Can some suggest how to fix this? > > Yes. Generally, most of the win32 services work like this: - the main thread listens to win32 service commands - when starting the service, you should create a new worker thread that does the job for you - when stopping the service, your service should report win32service.SERVICE_STOP_PENDING immediately, and ask the worker thread to terminate - you should be continuously reporting win32service.SERVICE_STOP_PENDING until your workder thread has stopped Here is a simple module that uses a 'Processor' class and a new thread to do the work. (The full program is here: http://mess.hu/download/SimpleHTTPService.zip ) class Service(win32serviceutil.ServiceFramework): _svc_name_ = SERVICE_NAME _svc_display_name_ = SERVICE_DISPLAY def __init__(self, args): win32serviceutil.ServiceFramework.__init__(self, args) self.stopped = threading.Event() self.stopped.clear() self.logger = getLogger(SERVICE_NAME) def SvcStop(self): self.logger.info("Got SvcStop, trying to stop service...") self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) self.stopped.set() def SvcDoRun(self): """Write an event log record - in debug mode we will also see this message printed.""" try: import servicemanager servicemanager.LogMsg( servicemanager.EVENTLOG_INFORMATION_TYPE, servicemanager.PYS_SERVICE_STARTED, (self._svc_name_, '') ) self.logger.info("Started.") self.logger.debug("Creating processor instance") processor = Processor(self.stopped) self.logger.debug("Starting processor thread") thread.start_new_thread(processor.Process,()) self.logger.debug("Waiting for the processor thread to finish") self.stopped.wait() self.logger.debug("Stopping") time.sleep(1) while not processor.stopped.isSet(): self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING, 5000) time.sleep(5) servicemanager.LogMsg( servicemanager.EVENTLOG_INFORMATION_TYPE, servicemanager.PYS_SERVICE_STOPPED, (self._svc_name_, "") ) self.logger.info("Stopped") except: self.logger.error('',exc_info = sys.exc_info()) if __name__=='__main__': win32serviceutil.HandleCommandLine(Service) -- http://mail.python.org/mailman/listinfo/python-list
Re: win32 service and time.sleep()
rbt wrote: > I have a win32 service written in Python. It works well. It sends a > report of the status of the machine via email periodically. The one > problem I have is this... while trying to send an email, the script > loops until a send happens and then it breaks. Should it be unable to > send, it sleeps for 10 minutes with time.sleep(600) and then wakes and > tries again. This is when the problem occurs. I can't stop the service > while the program is sleeping. When I try, it just hangs until a reboot. > Can some suggest how to fix this? One way would be to maintain a Queue.Queue containing the messages that need to be sent and, rather than sleeping to retry just retrying when enough time has elapsed. That way you can keep pumping messages and so on to your heart's content, though you will have to keep track of the messages still to be sent. regards Steve -- Steve Holden +44 150 684 7255 +1 800 494 3119 Holden Web LLC www.holdenweb.com PyCon TX 2006 www.pycon.org -- http://mail.python.org/mailman/listinfo/python-list
Re: win32 service and time.sleep()
Oracle wrote: > On Tue, 20 Sep 2005 10:49:13 -0400, rbt wrote: > >> I have a win32 service written in Python. It works well. It sends a >> report of the status of the machine via email periodically. The one >> problem I have is this... while trying to send an email, the script >> loops until a send happens and then it breaks. Should it be unable to >> send, it sleeps for 10 minutes with time.sleep(600) and then wakes and >> tries again. This is when the problem occurs. I can't stop the service >> while the program is sleeping. When I try, it just hangs until a reboot. >> Can some suggest how to fix this? >> > > You could try doing it the hard way. In a loop, sleep for 1-5 seconds at > a time. Everytime you complete a sleep, check the elapsed time. If the > elapsed time is >= the total sleep duration, fall out of the sleep loop > and try your email again. This should allow your service to stop within > 1-5 seconds of your request while imposing little to no load on the system. I adopted that solution just last week. When you wake (every few seconds), you check for a stop flag (and exit if needed of course) before checking if its time to do work-work. Works a treat. Another option is to use a threading.Condition, and have the service thread wait(600). The thread that calls to stop the service can set the stop flag and then notify() the Condition. Steve -- http://mail.python.org/mailman/listinfo/python-list
win32 Service: path to .py script
Hi, I have a testservice.py (see below). I installed the Windows-Service successfully. (via commandlineoption install) The problem is that it runs only when it is in c:\windows\system32 or in the python path. I added the desired path (Y:\) to the PYTHONPATH environment variable for the system account and rebooted. When I start command line python and do a import testservice everything is fine. Starting the service with the script only in Y:\ gives the error in the event log: Python could not import the service's module exceptions.ImportError: No module named testservice Thanks for your help. -- Greg testservice.py: import win32serviceutil, win32service import servicemanager import win32api import win32event import sys class TestPipeService(win32serviceutil.ServiceFramework): _svc_name_ = "MDE Dispatcher" _svc_display_name_ = "MDE-Dispatcher" _svc_description_ = "Dispatches machine events from the Siemens ODC to registrered MDE clients (windows) over the network via XML-RPC" def __init__(self, args): win32serviceutil.ServiceFramework.__init__(self, args) self.hWaitStop = win32event.CreateEvent(None, 0, 0, None) def SvcStop(self): self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) win32event.SetEvent(self.hWaitStop) def SvcDoRun(self): # Write an event log record - in debug mode we will also # see this message printed. servicemanager.LogMsg( servicemanager.EVENTLOG_INFORMATION_TYPE, servicemanager.PYS_SERVICE_STARTED, (self._svc_name_, '') ) while 1: win32api.Sleep(2) rc = win32event.WaitForSingleObject(self.hWaitStop, 2000) if rc == win32event.WAIT_OBJECT_0: break # Write another event log record. servicemanager.LogMsg( servicemanager.EVENTLOG_INFORMATION_TYPE, servicemanager.PYS_SERVICE_STOPPED, (self._svc_name_, " TEST ") ) if __name__=='__main__': win32serviceutil.HandleCommandLine(TestPipeService) -- http://mail.python.org/mailman/listinfo/python-list
Re: win32 Service: path to .py script
Gregor Horvath wrote: > Hi, > > I have a testservice.py (see below). I installed the Windows-Service > successfully. (via commandlineoption install) > The problem is that it runs only when it is in c:\windows\system32 or in > the python path. > I added the desired path (Y:\) to the PYTHONPATH environment variable > for the system account and rebooted. > When I start command line python and do a import testservice everything > is fine. > Starting the service with the script only in Y:\ gives the error in the > event log: > > Python could not import the service's module > > exceptions.ImportError: No module named testservice > > Thanks for your help. > > -- > Greg > > testservice.py: > > import win32serviceutil, win32service > import servicemanager > import win32api > import win32event > import sys > > class TestPipeService(win32serviceutil.ServiceFramework): > _svc_name_ = "MDE Dispatcher" > _svc_display_name_ = "MDE-Dispatcher" > _svc_description_ = "Dispatches machine events from the Siemens ODC > to registrered MDE clients (windows) over the network via XML-RPC" > def __init__(self, args): > win32serviceutil.ServiceFramework.__init__(self, args) > self.hWaitStop = win32event.CreateEvent(None, 0, 0, None) > > def SvcStop(self): > self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) > win32event.SetEvent(self.hWaitStop) > > > def SvcDoRun(self): > # Write an event log record - in debug mode we will also > # see this message printed. > servicemanager.LogMsg( > servicemanager.EVENTLOG_INFORMATION_TYPE, > servicemanager.PYS_SERVICE_STARTED, > (self._svc_name_, '') > ) > > while 1: > win32api.Sleep(2) > rc = win32event.WaitForSingleObject(self.hWaitStop, 2000) > if rc == win32event.WAIT_OBJECT_0: > break > > > # Write another event log record. > servicemanager.LogMsg( > servicemanager.EVENTLOG_INFORMATION_TYPE, > servicemanager.PYS_SERVICE_STOPPED, > (self._svc_name_, " TEST ") > ) > > > if __name__=='__main__': > win32serviceutil.HandleCommandLine(TestPipeService) > I believe that your problem is that services run under Local System account. Normally Local System account would not have a drive mapping Y:\. You can change the account that a service runs under in the Service Manager-Log On tab. Normally you wouldn't run services from mapped drives, they would be installed on local machine and started from there. If services need to access mapped drives, you need to put full UNC pathnames to access them or run the services under an account that has the drives mapped properly. -Larry Bates -- http://mail.python.org/mailman/listinfo/python-list
Re: win32 Service: path to .py script
Larry Bates schrieb: > I believe that your problem is that services run under Local > System account. Normally Local System account would not have > a drive mapping Y:\. You can change the account that a service You are absolutly correct. I moved the script to a local drive instead of a mapped network drive, now it works perfectly. Thank you a lot. -- Servus, Gregor -- http://mail.python.org/mailman/listinfo/python-list