Python and Windows Services Question

P

Phoe6

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)
 
T

Tim Golden

Phoe6 said:
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.


Just a quickie without looking at the code in any detail:
as a rule, Services can't interact with the desktop and
may not be running as any particular user. (Any desktop
user, in any case). While it *is* possible to enable that
interaction (altho' harder and harder as in Vista & W7)
this program is probably a good candidate for something
running silently under the logged-in user. (If you're
leaving your machine on in any case for this to run,
you might as well leave yourself logged in and screen-locked).

TJG
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top