- PyTivo
[pyTivo.git] / pyTivoService.py
blob2f9f6be0db5f62659c9dbe4ca72c306e543a7bb6
1 import beacon, httpserver, ConfigParser
2 import win32serviceutil
3 import win32service
4 import win32event
5 import select, sys
6 import Config
8 class PyTivoService(win32serviceutil.ServiceFramework):
9 _svc_name_ = 'pyTivo'
10 _svc_display_name_ = 'pyTivo'
12 def __init__(self, args):
13 win32serviceutil.ServiceFramework.__init__(self, args)
14 self.stop_event = win32event.CreateEvent(None, 0, 0, None)
16 def SvcDoRun(self):
18 import sys, os
20 from Config import config
22 p = os.path.dirname(__file__)
24 f = open(os.path.join(p, 'log.txt'), 'w')
25 sys.stdout = f
26 sys.stderr = f
29 port = config.get('Server', 'Port')
31 httpd = httpserver.TivoHTTPServer(('', int(port)), httpserver.TivoHTTPHandler)
33 for section in Config.getShares():
34 settings = {}
35 settings.update(config.items(section))
36 httpd.add_container(section, settings)
38 b = beacon.Beacon()
39 b.add_service('TiVoMediaServer:' + str(port) + '/http')
40 b.start()
42 while 1:
43 sys.stdout.flush()
44 (rx, tx, er) = select.select((httpd,), (), (), 5)
45 for sck in rx:
46 sck.handle_request()
47 rc = win32event.WaitForSingleObject(self.stop_event, 5)
48 if rc == win32event.WAIT_OBJECT_0:
49 b.stop()
50 break
52 def SvcStop(self):
53 win32event.SetEvent(self.stop_event)
55 if __name__ == '__main__':
56 win32serviceutil.HandleCommandLine(PyTivoService)