pyTivo
[pyTivo/krkeegan.git] / pyTivoService.py
blob3d4a9a7af937e1848f934b9983fcc43c40b71ee2
1 import beacon, httpserver, ConfigParser
2 import win32serviceutil
3 import win32service
4 import win32event
5 import select
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__)
20 open(os.path.join(p, '/', 'pyTivo.conf'))
22 config = ConfigParser.ConfigParser()
23 config.read( os.path.join(p, 'pyTivo.conf') )
25 port = config.get('Server', 'Port')
27 httpd = httpserver.TivoHTTPServer(('', int(port)), httpserver.TivoHTTPHandler)
29 for section in config.sections():
30 if not section == 'Server':
31 httpd.add_container(section, config.get(section, 'type'), config.get(section, 'path'))
33 b = beacon.Beacon()
34 b.add_service('TiVoMediaServer:' + str(port) + '/http')
35 b.send_beacon_timer()
37 while 1:
38 (rx, tx, er) = select.select((httpd,), (), (), 5)
39 for sck in rx:
40 sck.handle_request()
41 rc = win32event.WaitForSingleObject(self.stop_event, 5)
42 if rc == win32event.WAIT_OBJECT_0:
43 break
45 def SvcStop(self):
46 win32event.SetEvent(self.stop_event)
48 if __name__ == '__main__':
49 win32serviceutil.HandleCommandLine(PyTivoService)