Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Description Field in WinXP Services

Reply
Thread Tools

Description Field in WinXP Services

 
 
rbt
Guest
Posts: n/a
 
      01-29-2005
How does one associate a "Description" with a Windows service written in
Python? I've just started experimenting with Python services. Here's my
code... copied straight from Mr. Hammond's "Python Programming on Win32":

import win32serviceutil
import win32service
import win32event

class test_py_service(win32serviceutil.ServiceFramework) :
_svc_name_ = "test_py_service"
## Tried the following to no avail.
_svc_description_ = "Test written by Brad"
_svc_display_name_ = "Test Python Service"

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):
win32event.WaitForSingleObject(self.hWaitStop,
win32event.INFINITE)


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

 
Reply With Quote
 
 
 
 
Roger Upole
Guest
Posts: n/a
 
      01-29-2005
ChangeServiceConfig2 is the api functions that sets the description,
but it's not in the win32service module (yet).

Roger

"rbt" <> wrote in message
news:ctgij1$diq$...
> How does one associate a "Description" with a Windows service written in
> Python? I've just started experimenting with Python services. Here's my
> code... copied straight from Mr. Hammond's "Python Programming on Win32":
>
> import win32serviceutil
> import win32service
> import win32event
>
> class test_py_service(win32serviceutil.ServiceFramework) :
> _svc_name_ = "test_py_service"
> ## Tried the following to no avail.
> _svc_description_ = "Test written by Brad"
> _svc_display_name_ = "Test Python Service"
>
> 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):
> win32event.WaitForSingleObject(self.hWaitStop,
> win32event.INFINITE)
>
>
> if __name__ == '__main__':
> win32serviceutil.HandleCommandLine(test_py_service )
>





----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---
 
Reply With Quote
 
 
 
 
rbt
Guest
Posts: n/a
 
      01-30-2005
Roger Upole wrote:
> ChangeServiceConfig2 is the api functions that sets the description,
> but it's not in the win32service module (yet).
>
> Roger


OK, I can use _winreg to add the 'Description' field under the
appropriate registry key.
 
Reply With Quote
 
rbt
Guest
Posts: n/a
 
      01-30-2005
rbt wrote:
> Roger Upole wrote:
>
>> ChangeServiceConfig2 is the api functions that sets the description,
>> but it's not in the win32service module (yet).
>>
>> Roger

>
>
> OK, I can use _winreg to add the 'Description' field under the
> appropriate registry key.


Here's an example of it... kludgey but it works

from _winreg import *
import time

def Svc_Description():
try:
key_location = r"SYSTEM\CurrentControlSet\Services\test_py_servic e"
svc_key = OpenKey(HKEY_LOCAL_MACHINE, key_location, 0, KEY_ALL_ACCESS)
SetValueEx(svc_key,'Description',0,REG_SZ,u"Brad's Test Python Service")
CloseKey(svc_key)
except Exception, e:
print e

Svc_Description()
time.sleep(10)
 
Reply With Quote
 
Larry Bates
Guest
Posts: n/a
 
      01-31-2005
Roger,

I wrote an extension to Mark Hammond's win32serviceutil.ServiceFramework class
(that I submitted to him also) that extends the class to provide you with this
functionality (along with the ability to support py2exe frozen services better).

Here it is:

import _winreg
import os
import win32evtlogutil

class NTserviceBase:
'''
Written by: Larry A. Bates - Syscon, Inc. March 2004

This is a base class for creating new NTservices with Python.
It should be included when you are defining your class as follows:

class newNTservice(win32serviceutil.ServiceFramework, NTserviceBase):
#
# Required attributes for a service
#
_svc_name_="newNTservice"
_svc_display_name_="newNTservice"
_svc_description_='newNTservice is a service that can be installed ' \
'and 'run on any Windows NT, 2000, XP, 2003 ' \

'computer. It can be controlled by services applet' \
'in the Control Panel and will run unattended ' \
'after being started.'

def __init__(self, args):
NTserviceBase.__init__(self)
win32serviceutil.ServiceFramework.__init__(self, args)
#
# Create an event which we will use to wait on.
# The "service stop" request will set this event.
#
self.hWaitStop=win32event.CreateEvent(None, 0, 0, None)
return

Note: the __init__ method of the created class should call the __init__
method of NTserviceBase AND win32serviceutil.ServiceFramework to get
everything initialized properly
'''
def __init__(self):
#-------------------------------------------------------------------
# Call function to set self.PythonService (0=binary, 1=Python)
#-------------------------------------------------------------------
self.getSERVICEtype()
self.installpath=self.getSERVICEpath()
#-------------------------------------------------------------------
# Call function to set Service's long description string. Can't
# seem to find a way to do this that works for both PythonServices
# and binary (py2exe) services. So I will set the description
# every time the service is started.
#-------------------------------------------------------------------
self.setSERVICEdescription()
#-------------------------------------------------------------------
# Call function to register eventlog message handler which is
# different if I'm a binary vs. PythonService.
#-------------------------------------------------------------------
self.getPythonServicePath()
#-------------------------------------------------------------------
# If this is a PythonService, point to installed PythonService.exe
# if not, point to a copy of PythonService.exe that gets installed
# along with the binary. Note: on binary distributions you must
# include a copy of PythonService.exe along with your other files
# to provide EventLog message decoding.
#-------------------------------------------------------------------
if self.PythonService: sourcepath=self.PythonServicePath
else: sourcepath=os.path.join(self.installpath, "PythonService.exe ")
self.setSERVICEeventlogSource(sourcepath)
return

def getSERVICEtype(self):
'''
Function to determine if this is a Python service or a binary service
created by py2exe. Sets self.PythonService=1 if it is a Python service
or PythonService=0 if it is a binary service.
'''
#---------------------------------------------------------------------
# This service can run as a PythonService or it can run as a binary
# .EXE service (created by py2exe). We can sense this by looking
# to see if
# HKLM\SYSTEM\CurrentControlSet\Services\_svc_name_\ PythonClass
# key exists in the registry. If it doesn't, then I'm a binary
# install.
#---------------------------------------------------------------------
# Open registry and search for PythonService install
#---------------------------------------------------------------------
regkey='SYSTEM\CurrentControlSet\Services\%s' % self._svc_name_
key=_winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, regkey)
nsubkeys, nvalues, lastmodified=_winreg.QueryInfoKey(key)
#---------------------------------------------------------------------
# Begin by assuming I'm a binary (non-Python) service created by py2exe
#---------------------------------------------------------------------
self.PythonService=0
#---------------------------------------------------------------------
# Loop over the keys in this branch looking for "PythonService" key
#---------------------------------------------------------------------
for i in range(nsubkeys):
subkeyname=_winreg.EnumKey(key, i)
if subkeyname == 'PythonClass':
self.PythonService=1
break

return

def getSERVICEpath(self):
'''
Function to retrieve the full pathname to where the service has been
installed on the machine. This can be used to locate .INI or other
data files (if this service uses any).
'''
if self.PythonService:
regkey='SYSTEM\CurrentControlSet\Services\%s\Pytho nClass' \
% self._svc_name_
value_name='' # Default
else:
regkey='SYSTEM\CurrentControlSet\Services\%s' % self._svc_name_
value_name="ImagePath"

#---------------------------------------------------------------------
# Get the contents of the value_name value under this key. This
# contains the full pathname where the service was installed and
# points me to the .INI file.
#---------------------------------------------------------------------
key=_winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, regkey)
SERVICEpath=_winreg.QueryValueEx(key, value_name)[0]
#---------------------------------------------------------------------
# Extract the full pathname where this service has been installed from
# this registry entry so I can locate the .INI file.
#---------------------------------------------------------------------
SERVICEpath=str(os.path.split(SERVICEpath)[0])
return SERVICEpath

def getPythonServicePath(self):
'''
Function to retrieve the full pathname to where PythonService.exe has
been installed on the machine (only used for Python Services, not binary
ones). This can be used to locate PythonService.exe for EventLog
message decoding.
'''
self.PythonService=None
regkey='SOFTWARE\Python\PythonService'
try: key=_winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, regkey)
except: return
#---------------------------------------------------------------------
# Get first subkeyname (Python Version)
#---------------------------------------------------------------------
subkeyname=_winreg.EnumKey(key, 0)
#---------------------------------------------------------------------
# Get the contents of the value_name value under this key. This
# contains the full pathname where the PythonService.exe is installed.
#---------------------------------------------------------------------
self.PythonServicePath=_winreg.QueryValue(key, subkeyname)
return

def setSERVICEdescription(self):
'''
This function sets the long description string that is displayed in the
Control Panel applet when this service is selected to the contents of
self._svc_description_ defined at the top of the service.
'''
#---------------------------------------------------------------------
# Open registry at the proper key
#---------------------------------------------------------------------
regkey='SYSTEM\CurrentControlSet\Services\%s' % self._svc_name_
key=_winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, regkey, 0, \
_winreg.KEY_SET_VALUE)

_winreg.SetValueEx(key, 'Description', 0, _winreg.REG_SZ, \
self._svc_description_)
return

def setSERVICEeventlogSource(self, sourcepath):
'''
This function sets the path to PythonService.exe so that EventLog
messages can be properly decoded.
'''
win32evtlogutil.AddSourceToRegistry(self._svc_name _, sourcepath,
'Application')
return


Hope it helps,
Larry Bates



rbt wrote:
> rbt wrote:
>
>> Roger Upole wrote:
>>
>>> ChangeServiceConfig2 is the api functions that sets the description,
>>> but it's not in the win32service module (yet).
>>>
>>> Roger

>>
>>
>>
>> OK, I can use _winreg to add the 'Description' field under the
>> appropriate registry key.

>
>
> Here's an example of it... kludgey but it works
>
> from _winreg import *
> import time
>
> def Svc_Description():
> try:
> key_location = r"SYSTEM\CurrentControlSet\Services\test_py_servic e"
> svc_key = OpenKey(HKEY_LOCAL_MACHINE, key_location, 0,
> KEY_ALL_ACCESS)
> SetValueEx(svc_key,'Description',0,REG_SZ,u"Brad's Test Python
> Service")
> CloseKey(svc_key)
> except Exception, e:
> print e
>
> Svc_Description()
> time.sleep(10)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
javascript validation for a not required field, field is onlyrequired if another field has a value jr Javascript 3 07-08-2010 10:33 AM
System.Web.Services.WebMethod( Description:= ...) Mark B ASP .Net 2 05-07-2009 09:46 AM
1.Enter space bar for field names and save the field.The field shoud not get saved and an alert should be there as"Space bars are not allowed" Sound Javascript 2 09-28-2006 02:43 PM
Parsing WSDL and System.Web.Services.Description Chris Bardon ASP .Net Web Services 0 05-17-2004 04:18 PM
Using GetOleDbSchemaTable to get SQL Server Field Description - using pete ASP .Net 1 08-29-2003 10:50 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57