Python实现windows Service服务程序. Win32serviceutil.ServiceFramework是封装得很好的Windows服务框架,本文通过继承它来实现。 通过SvcDoRun方法,实现服务启动,运行服务内的业务代码。 通过SvcStop方法,停止服务。 WinPollManager.py代码如下:. No Module Named Win32Serviceutil Download And Install. Set to weekly or daily to automatically download and install the latest Insiders builds of the Python extension, which include upcoming features and bug fixes. Where Python is concerned, ctags makes it easier to jump to defined functions and other symbols in CC extension modules. Since the executable includes a Python interpreter and all needed: packages, so we recommend that approach. A typical sequence of commands to test the service of this module is: exampleservice.exe install: exampleservice.exe start: exampleservice.exe stop: exampleservice.exe remove: For more complete usage information, invoke the executable. This is pretty simple – if there is a single argument (the script/binary itself), then start the service control dispatcher to allow Windows to manage the service. If there are any other arguments, assume that it is arguments to manager the service, e.g. Install, or start. In both cases, we send our ServiceFramework subclass as a parameter. For Python you can do this, which creates the service in one go: nssm install MyServiceName c: python27 python.exe c: temp myscript.py. Where myscript.py is the boilerplate script above, modified to invoke your application script or code in the main function.
The advantage of the program is going to run on system boot.
Python Win32serviceutil Install Service Pack
So basically a program or software that has to run non stop without any interruption.
import win32serviceimport win32serviceutil
import win32api
import win32con
import win32event
import win32evtlogutil
import os, sys, string, time
class aservice(win32serviceutil.ServiceFramework):
Install Windows Service
_svc_name_ = “PIDS Alarms”
_svc_display_name_ = “Commtel SMS”
_svc_description_ = “Sending SMS via GSM dongle to send out alert!”
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):
import servicemanager
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,servicemanager.PYS_SERVICE_STARTED,(self._svc_name_, ”))
#self.timeout = 640000 #640 seconds / 10 minutes (value is in milliseconds)
self.timeout = 120000 #120 seconds / 2 minutes
# This is how long the service will wait to run / refresh itself (see script below)
while 1:
# Wait for service stop signal, if I timeout, loop again
rc = win32event.WaitForSingleObject(self.hWaitStop, self.timeout)
# Check to see if self.hWaitStop happened
if rc win32event.WAIT_OBJECT_0:
# Stop signal encountered
servicemanager.LogInfoMsg(“SomeShortNameVersion – STOPPED!”) #For Event Log
break
else:
#Ok, here’s the real money shot right here.
#[actual service code between rests]
try:
file_path = r”C:yourfile.py”
execfile(file_path) #Execute the script
inc_file_path2 = r”C:PIDScommtel_txt.py”
execfile(inc_file_path2) #Execute the script
except:
pass
#[actual service code between rests]
def ctrlHandler(ctrlType):
return True
Python Win32serviceutil Install
if __name__ ‘__main__’:
win32api.SetConsoleCtrlHandler(ctrlHandler, True)
win32serviceutil.HandleCommandLine(aservice)
Save the above code in start.py
The you have three options through command prompt where you can install, start, stop the service
C:>python start.py install (This install the commtel_sms.py as a service)
C:>python start.py start (This start the commtel_sms.py as a service)
C:>python start.py stop (This stop the commtel_sms.py as a service)
I have a very simple win32serviceutil script:Looks like Microsoft thinks you mis-spelled it.
import win32serviceutil, time
win32serviceutil.StartService('burek', 'localhost')
time.sleep(10)
exit()
It successfuly imports win32serviceutil, and chokes on StartService:
Traceback (most recent call last):
File 'foobar.py', line 3, in ?
win32serviceutil.StartService('burek', 'localhost')
File 'C:Python24Libsite-packageswin32libwin32serviceutil.py', line
399, in StartService
hs = SmartOpenService(hscm, serviceName, win32service.SERVICE_ALL_ACCESS)
File 'C:Python24Libsite-packageswin32libwin32serviceutil.py', line
76, in SmartOpenService
return win32service.OpenService(hscm, name, access)
pywintypes.error: (1060, 'OpenService', 'The specified service does not
exist as an installed service.')
What does that mean?
--
'Now the storm has passed over me
I'm left to drift on a dead calm sea
And watch her forever through the cracks in the beams
Nailed across the doorways of the bedrooms of my dreams'
http://www.microsoft.com/technet/pro....mspx?mfr=true
I would check and see if that service is installed on your PC. You can
go to Start --Run and type services.msc
Scroll through there and see if your service is listed. You might
check to see if you can enable/disable it via that console as well.
Mike