pyTivo
[pyTivo.git] / pyTivoService.py
bloba1e79fa5048dda3c9877549a18640c5acc5cb9c1
1 import beacon, httpserver, ConfigParser
2 import win32serviceutil
3 import win32service
4 import win32event
5 import select, sys
7 class PyTivoService(win32serviceutil.ServiceFramework):
8 _svc_name_ = 'pyTivo'
9 _svc_display_name_ = 'pyTivo'
11 def __init__(self, args):
12 win32serviceutil.ServiceFramework.__init__(self, args)
13 self.stop_event = win32event.CreateEvent(None, 0, 0, None)
15 def SvcDoRun(self):
17 import sys, os
19 p = os.path.dirname(__file__)
21 f = open(os.path.join(p, 'log.txt'), 'w')
22 sys.stdout = f
23 sys.stderr = f
24 print 'test'
26 config = ConfigParser.ConfigParser()
27 config.read( os.path.join(p, 'pyTivo.conf') )
29 port = config.get('Server', 'Port')
31 httpd = httpserver.TivoHTTPServer(('', int(port)), httpserver.TivoHTTPHandler)
33 for section in config.sections():
34 if not section == 'Server':
35 httpd.add_container(section, config.get(section, 'type'), config.get(section, 'path'))
37 b = beacon.Beacon()
38 b.add_service('TiVoMediaServer:' + str(port) + '/http')
39 b.send_beacon_timer()
41 while 1:
42 sys.stdout.flush()
43 (rx, tx, er) = select.select((httpd,), (), (), 5)
44 for sck in rx:
45 sck.handle_request()
46 rc = win32event.WaitForSingleObject(self.stop_event, 5)
47 if rc == win32event.WAIT_OBJECT_0:
48 break
50 def SvcStop(self):
51 win32event.SetEvent(self.stop_event)
53 if __name__ == '__main__':
54 win32serviceutil.HandleCommandLine(PyTivoService)