-
Notifications
You must be signed in to change notification settings - Fork 59
/
pyTivoService.py
53 lines (39 loc) · 1.45 KB
/
pyTivoService.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import beacon, httpserver
import win32serviceutil
import win32service
import win32event
import select, sys
import config
class PyTivoService(win32serviceutil.ServiceFramework):
_svc_name_ = 'pyTivo'
_svc_display_name_ = 'pyTivo'
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.stop_event = win32event.CreateEvent(None, 0, 0, None)
def SvcDoRun(self):
import sys, os
p = os.path.dirname(__file__)
f = open(os.path.join(p, 'log.txt'), 'w')
sys.stdout = f
sys.stderr = f
port = config.getPort()
httpd = httpserver.TivoHTTPServer(('', int(port)),
httpserver.TivoHTTPHandler)
for section, settings in config.getShares():
httpd.add_container(section, settings)
b = beacon.Beacon()
b.add_service('TiVoMediaServer:' + str(port) + '/http')
b.start()
while 1:
sys.stdout.flush()
(rx, tx, er) = select.select((httpd,), (), (), 5)
for sck in rx:
sck.handle_request()
rc = win32event.WaitForSingleObject(self.stop_event, 5)
if rc == win32event.WAIT_OBJECT_0:
b.stop()
break
def SvcStop(self):
win32event.SetEvent(self.stop_event)
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(PyTivoService)