Python and Windows Services Question

Phoe6 orsenthil at gmail.com
Wed Mar 4 06:26:46 EST 2009


Hello,

This is a windows services question. The following script which is a
windows service was supposed to something ( download wallpaper and set
the desktopwallpaper) at 16:45 my time here. The service is running,
but the supposed action did not take place. Does anyone who is aware
of win32 apis point me what I am doing wrong here: http://paste.pocoo.org/show/106428/

I don't know much about Win32 Services. So I took the example snippet
from Mark Hammond's book on Python Windows programming.

Any help appreciated.

Thanks,
Senthil


# Code Inline
# Download and setwallpaper is my code.


import os
import re
import urllib2
import time
import Image
import win32gui
import win32serviceutil
import win32service
import win32event
import datetime

from win32con import SPI_SETDESKWALLPAPER

def setwallpaper():
	'''A function which downloads the wallpaper of the day and sets it up
as desktop wallpaper.'''

	siteurl = 'http://www.nationalgeographic.com/photography/today/'
	req = urllib2.Request(siteurl)
	data = urllib2.urlopen(req).read()

	ptrn = re.compile('/photography/wallpaper/.*\\.html')
	mtch = ptrn.search(data)

	wallpaper_url = 'http://photography.nationalgeographic.com' + str
(mtch.group())

	req = urllib2.Request(wallpaper_url)
	data = urllib2.urlopen(req)
	content = data.read()

	lookfor = re.compile('href="(\\S*-lw\\.jpg)')
	picture = lookfor.search(content).group(1)
	wallpaper = 'http://photography.nationalgeographic.com' + str
(picture)

	req = urllib2.Request(wallpaper)
	data = urllib2.urlopen(req).read()
	imgfile = open('wallpaper.jpg','wb')
	imgfile.write(data)
	imgfile.close()
	bmpimgfile = 'wallpaper.bmp'
	Image.open('wallpaper.jpg').save(bmpimgfile,'BMP')
	path = os.getcwd() + os.sep + bmpimgfile

	win32gui.SystemParametersInfo(SPI_SETDESKWALLPAPER, path, 0)



class SmallestPythonService(win32serviceutil.ServiceFramework):
    _svc_name_ = "SmallestPythonService"
    _svc_display_name_ = "Wallpaper Service"
    def __init__(self, args):
        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)

    def SvcStop(self):
        # Before we do anything, tell the SCM we are starting the stop
process.
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        # And set my event.
        win32event.SetEvent(self.hWaitStop)

    def SvcDoRun(self):
        # We do nothing other than wait to be stopped!

	# If the time is 5 hours ( 5pm of the day), run the setwallpaper
function
	if  time.localtime()[3] == 16 and time.localtime()[4] == 45:
		setwallpaper()
        win32event.WaitForSingleObject(self.hWaitStop,
win32event.INFINITE)

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



More information about the Python-list mailing list