/
controller.py
302 lines (259 loc) · 12.1 KB
/
controller.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/env python
#! coding: utf-8
import sys, os
sys.path.append(os.path.dirname(__file__))
from datetime import datetime
from lib.cache import KeyValueDatabase
from lib.tornado import wsgi, httpserver, ioloop
from lib.text_processing import pretty_xml
from settings import api_db
from api.error import Error
from api.dictionary import Dictionary
from api.authentication import Authentication
from api.course import Course
import time
import urllib
# Use UTF-8 for output
reload(sys)
sys.setdefaultencoding('utf-8') # IGNORE:E1101
#----- description text with last update -----
## check .py file in api folder and this file to detect last_update
last_update = []
for i in os.listdir('api'):
s = os.stat(os.path.join('api', i))
_last_update = s.st_mtime
last_update.append(_last_update)
s = os.stat(__file__)
_last_update = s.st_mtime
last_update.append(_last_update)
last_update = time.strftime("%d-%m-%Y", time.localtime(max(last_update)))
#----- Description Header ------
__description__ = '<api version="0.9.1" last_update="%s">' % (last_update)
__description__ = __description__ + '%s</api>'
#----- Global Parameters -----
API_DB = KeyValueDatabase(api_db)
API_KEY = 'c74df88f0b1db2e4f48fdc4903851d41'
status = '200 OK'
#----- Map URI to functions ------
class URIMapper:
def __init__(self):
self.dict = Dictionary()
self.authen = Authentication()
self.course = Course()
self.error = Error()
def execute(self, environ):
path = environ['PATH_INFO']
#----- dictionary group -----
if path.startswith('/lookup:'):
return self.dict.lookup(environ)
#----- authentication group -----
elif path.startswith('/register:'):
return self.authen.register(environ)
elif path.startswith('/login:'):
return self.authen.login(environ)
elif path.startswith('/logout:'):
return self.authen.logout(environ)
#----- learning group -----
elif path.startswith('/levels:'):
return self.course.levels(environ)
elif path.startswith('/lessons:'):
return self.course.lessons(environ)
elif path.startswith('/listen:'):
return self.course.listen(environ)
else:
return self.error.bad_request()
#----- API Controller -----
class Handler:
"""Bộ điều khiển truy vấn và xác thực qua API.
Các API key được quản lý qua công cụ api_manager trong gói admin kèm trong
mã nguồn.
Theo mặc định, API key chỉ có duy nhất 1 và không giới hạn lượt truy cập và
độ trễ giữa mỗi lần truy câp.
"""
def __init__(self):
self.m = URIMapper()
self.error = Error()
def _parse_request(self, path):
try:
info = path.split(':')
request = info[1][:-1].replace("'&", '"&') # Normalize
params = {}
for keyword in request.split('"&'):
pair = keyword.split("=")
params[pair[0]] = urllib.unquote(pair[1][1:])
return params
except:
return 400 # Bad request
def request_handler(self, environ, start_response):
try:
# check method
if environ['REQUEST_METHOD'] != 'GET':
info = '<error status_code="405" \
description="Phương thức này không được hệ thống hỗ trợ"/>'
response_headers = [('Content-type', 'text/xml'),
('Content-Length', str(len(info)))]
start_response(status, response_headers)
return [info]
#-- get uri --
path = environ['PATH_INFO'].lstrip('/')
path = urllib.unquote(path)
# some tweak :-)
if path == 'favicon.ico':
response_headers = [('Content-type', 'text/xml'),
('Content-Length', 0)]
start_response(status, response_headers)
return ['']
if path.startswith('about'):
info = """<about author="AloneRoad"
full_name="Phạm Tuấn Anh"
email="AloneRoad@Gmail.com"
mobile_phone="+84 167 345 0 799"
message="Cảm ơn vì đã quan tâm ⍣ "/>"""
response_headers = [('Content-type', 'text/xml'),
('Content-Length', str(len(info)))]
start_response(status, response_headers)
return [info]
#------ for documents------
elif path == '': # return api developer's guide
doc = open(os.path.join(os.path.dirname(__file__),
'doc/build/html/index.html')).read()
response_headers = [('Content-type', 'text/html'),
('Content-Length', str(len(doc)))]
start_response(status, response_headers)
return [doc]
elif path in ['_static/nature.css',
'_static/pygments.css',
'_static/basic.css']:
doc = open(os.path.join(os.path.dirname(__file__),
'doc/build/html/', path)).read()
response_headers = [('Content-type', 'text/css'),
('Content-Length', str(len(doc)))]
start_response(status, response_headers)
return [doc]
elif path in ['_static/jquery.js',
'_static/doctools.js',
'_static/jquery.js']:
doc = open(os.path.join(os.path.dirname(__file__),
'doc/build/html/', path)).read()
response_headers = [('Content-type', 'text/javascript'),
('Content-Length', str(len(doc)))]
start_response(status, response_headers)
return [doc]
#----- end developer's guide -----
#----- get remote ip -----
ip_request = environ['REMOTE_ADDR']
ip_key = 'api::address="%s"?last_request' % ip_request
_time = datetime.now()
last_request = API_DB.get(ip_key) # get info
if last_request:
last_request = datetime.strptime(last_request, '%Y-%m-%d %H:%M:%S.%f')
else:
last_request = _time
API_DB.set(ip_key, str(_time)) # set new info
#----- Parse request -----
_params = self._parse_request(path)
if _params == 400:
xml = """<error status_code="400"
description="Yêu cầu không hợp lệ"/>"""
xml = __description__ % xml
xml = pretty_xml(xml)
response_headers = [('Content-type', 'text/xml'),
('Content-Length', str(len(xml)))]
start_response(status, response_headers)
return [xml]
else:
try:
key = 'api::api_key="%s"?last_call' % (_params['api_key'])
except KeyError:
xml = self.error.authen_error("Thiếu api_key")
xml = __description__ % xml
xml = pretty_xml(xml)
response_headers = [('Content-type', 'text/xml'),
('Content-Length', str(len(xml)))]
start_response(status, response_headers)
return [xml]
API_DB.set(key, _time)
total_key = 'api::api_key="%s"?total_request_per_day' % (_params['api_key'])
total = API_DB.get(total_key)
if total is None:
xml = """<error status_code="401"
description="api_key không hợp lệ"/>"""
xml = __description__ % xml
xml = pretty_xml(xml)
response_headers = [('Content-type', 'text/xml'),
('Content-Length', str(len(xml)))]
start_response(status, response_headers)
return [xml]
delay_key = 'api::api_key="%s"?delay' % (_params['api_key'])
limit = API_DB.get(delay_key)
total_call_key = 'api::api_key="%s"?total_call' % (_params['api_key'])
total_call = API_DB.get(total_call_key)
delta = _time - last_request
if delta.days != 0:
API_DB.set(total_call_key, 0)
# Check sum requests
if int(total_call) >= int(total) and int(total) != 0:
message = """
api_key này chỉ được phép sử dụng %s lần một ngày
""" % total
xml = """
<error status_code="403" description="%s"/>
""" % message
xml = __description__ % xml
xml = pretty_xml(xml)
response_headers = [('Content-type', 'text/xml'),
('Content-Length', str(len(xml)))]
start_response(status, response_headers)
return [xml]
# Limit time per request
elif (delta.seconds * 1e6 + delta.microseconds) < limit:
message = """
Khoảng thời gian tối thiểu giữa 2 lần request phải lớn hơn %s mili-giây
""" % (limit / 1000)
xml = """
<error status_code="403" description="%s"/>
""" % message
xml = __description__ % xml
xml = pretty_xml(xml)
response_headers = [('Content-type', 'text/xml'),
('Content-Length', str(len(xml)))]
start_response(status, response_headers)
return [xml]
else: # accept request
API_DB.set(total_call_key, int(total_call) + 1) # incr total call
environ['request'] = self._parse_request(path)
xml = self.m.execute(environ)
xml = __description__ % xml
xml = pretty_xml(xml)
response_headers = [('Content-type', 'text/xml'),
('Content-Length', str(len(xml)))]
start_response(status, response_headers)
return [xml]
# Debug off | (on when KeyboardInterrupt)
except KeyboardInterrupt: # pylint: disable-msg=W0702
message = """
Hệ thống gặp một lỗi chưa rõ nguyên nhân.
Vui lòng báo lại cho bộ phận quản trị nếu gặp thông báo này
"""
xml = """
<error status_code="500" description="%s"/>
""" % message
xml = __description__ % xml
response_headers = [('Content-type', 'text/xml'),
('Content-Length', str(len(xml)))]
start_response(status, response_headers)
return [xml]
if __name__ == '__main__':
try:
port = int(sys.argv[1])
except:
print """
Sử dụng: python controller.py port
Ví dụ:
python controller.py 8000
"""
sys.exit()
container = wsgi.WSGIContainer(Handler().request_handler)
http_server = httpserver.HTTPServer(container)
http_server.listen(port)
ioloop.IOLoop.instance().start()