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