-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.py
executable file
·125 lines (100 loc) · 3.18 KB
/
service.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# -*- coding: utf-8 -*-
import socketserver
# from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
# from urlparse import parse_qsl
import threading
import http.server
import urllib
server = None
Handler = http.server.BaseHTTPRequestHandler
class MyHandler(Handler):
global server
API = None
def do_GET(self):
import control
control.setbusy(True)
import tv
try:
if 'channel' in self.path:
req = self.path.replace('/channel?', '')
params = dict(urllib.parse.parse_qsl(req))
api = tv.GetInstance()
url = api.getTvSource('/tv/' + str(params['id']))
self.send_response(301)
self.send_header('Location', url)
self.end_headers()
self.finish()
except Exception as e:
control.sendError('Błąd inicjalizacji kanału TV')
control.logError(str(e))
control.setbusy(False)
class AsyncCall(object):
def __init__(self, fnc, callback=None):
self.Callable = fnc
self.Callback = callback
def __call__(self, *args, **kwargs):
self.Thread = threading.Thread(target=self.run, name=self.Callable.__name__, args=args, kwargs=kwargs)
self.Thread.start()
return self
def wait(self, timeout=None):
self.Thread.join(timeout)
if self.Thread.isAlive():
raise TimeoutError()
else:
return self.Result
def run(self, *args, **kwargs):
self.Result = self.Callable(*args, **kwargs)
if self.Callback:
self.Callback(self.Result)
class AsyncMethod(object):
def __init__(self, fnc, callback=None):
self.Callable = fnc
self.Callback = callback
def __call__(self, *args, **kwargs):
return AsyncCall(self.Callable, self.Callback)(*args, **kwargs)
def Async(fnc=None, callback=None):
if fnc == None:
def AddAsyncCallback(fnc):
return AsyncMethod(fnc, callback)
return AddAsyncCallback
else:
return AsyncMethod(fnc, callback)
@Async
def startServer():
global server;
import control
server_enable = control.server_enable;
if server_enable == 'true':
port = int(control.server_port);
try:
server = socketserver.TCPServer(('', port), MyHandler);
server.serve_forever();
except KeyboardInterrupt:
if server != None:
server.socket.close();
def stopServer():
import control
try:
url = urllib.urlopen('http://localhost:' + str(control.server_port) + '/stop')
code = url.getcode()
except Exception as e:
control.sendError(str(e))
return True
def serverOnline():
import control
try:
url = urllib.urlopen('http://localhost:' + str(control.server_port) + '/online')
code = url.getcode()
if code == 200:
return True
except Exception as e:
return False
return False
def initSession():
import client
client.authRequest('', True)
if __name__ == '__main__':
import control
if control.server_enable:
initSession()
startServer()