forked from armooo/pytivo
-
Notifications
You must be signed in to change notification settings - Fork 42
/
pyTivoService.py
48 lines (37 loc) · 1.21 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
import os
import select
import sys
import time
import win32event
import win32service
import win32serviceutil
import pyTivo
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 mainloop(self):
httpd = pyTivo.setup(True)
while True:
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 or httpd.stop:
break
httpd.beacon.stop()
return httpd.restart
def SvcDoRun(self):
p = os.path.dirname(__file__)
f = open(os.path.join(p, 'log.txt'), 'w')
sys.stdout = f
sys.stderr = f
while self.mainloop():
time.sleep(5)
def SvcStop(self):
win32event.SetEvent(self.stop_event)
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(PyTivoService)