Commit 1ecfefbf9b618852df816dc6c7a9c0d4d8a9b8bb
1 parent
a203d3cc
Exists in
master
and in
1 other branch
- removed commented code.
- added option --port to select the HTTPS port.
Showing
2 changed files
with
18 additions
and
43 deletions
Show diff stats
learnapp.py
| ... | ... | @@ -29,12 +29,16 @@ class LearnAppException(Exception): |
| 29 | 29 | # ============================================================================ |
| 30 | 30 | # helper functions |
| 31 | 31 | # ============================================================================ |
| 32 | -async def check_password(try_pw, pw): | |
| 32 | +async def _bcrypt_hash(a, b): | |
| 33 | 33 | loop = asyncio.get_running_loop() |
| 34 | - return pw == await loop.run_in_executor(None, bcrypt.hashpw, try_pw.encode('utf-8'), pw) | |
| 34 | + return await loop.run_in_executor(None, bcrypt.hashpw, a.encode('utf-8'), b) | |
| 35 | + | |
| 36 | +async def check_password(try_pw, pw): | |
| 37 | + return pw == await _bcrypt_hash(try_pw, pw) | |
| 38 | + | |
| 39 | +async def bcrypt_hash_gen(new_pw): | |
| 40 | + return await _bcrypt_hash(new_pw, bcrypt.gensalt()) | |
| 35 | 41 | |
| 36 | -def bcrypt_hash_gen(pw): | |
| 37 | - return bcrypt.hashpw(pw.encode('utf-8'), bcrypt.gensalt()) | |
| 38 | 42 | |
| 39 | 43 | # ============================================================================ |
| 40 | 44 | # LearnApp - application logic |
| ... | ... | @@ -115,14 +119,13 @@ class LearnApp(object): |
| 115 | 119 | logger.info(f'User "{uid}" logged out') |
| 116 | 120 | |
| 117 | 121 | # ------------------------------------------------------------------------ |
| 118 | - # change_password | |
| 122 | + # change_password. returns True if password is successfully changed. | |
| 119 | 123 | # ------------------------------------------------------------------------ |
| 120 | 124 | async def change_password(self, uid, pw): |
| 121 | 125 | if not pw: |
| 122 | 126 | return False |
| 123 | 127 | |
| 124 | - loop = asyncio.get_running_loop() | |
| 125 | - pw = await loop.run_in_executor(None, bcrypt_hash_gen, pw) | |
| 128 | + pw = await bcrypt_hash_gen(pw) | |
| 126 | 129 | |
| 127 | 130 | with self.db_session() as s: |
| 128 | 131 | u = s.query(Student).get(uid) | ... | ... |
serve.py
| ... | ... | @@ -16,7 +16,6 @@ import functools |
| 16 | 16 | import tornado.ioloop |
| 17 | 17 | import tornado.web |
| 18 | 18 | import tornado.httpserver |
| 19 | -# from tornado import iostream | |
| 20 | 19 | |
| 21 | 20 | # this project |
| 22 | 21 | from learnapp import LearnApp |
| ... | ... | @@ -34,6 +33,7 @@ def admin_only(func): |
| 34 | 33 | func(self, *args, **kwargs) |
| 35 | 34 | return wrapper |
| 36 | 35 | |
| 36 | + | |
| 37 | 37 | # ============================================================================ |
| 38 | 38 | # WebApplication - Tornado Web Server |
| 39 | 39 | # ============================================================================ |
| ... | ... | @@ -45,7 +45,6 @@ class WebApplication(tornado.web.Application): |
| 45 | 45 | (r'/change_password', ChangePasswordHandler), |
| 46 | 46 | (r'/question', QuestionHandler), # renders each question |
| 47 | 47 | (r'/topic/(.+)', TopicHandler), # page for exercising a topic |
| 48 | - # (r'/learn/(.+)', LearnHandler), # page for learning a topic | |
| 49 | 48 | (r'/file/(.+)', FileHandler), # serve files, images, etc |
| 50 | 49 | (r'/', RootHandler), # show list of topics |
| 51 | 50 | ] |
| ... | ... | @@ -159,24 +158,6 @@ class TopicHandler(BaseHandler): |
| 159 | 158 | name=self.learn.get_student_name(uid), |
| 160 | 159 | ) |
| 161 | 160 | |
| 162 | -# class LearnHandler(BaseHandler): | |
| 163 | -# @tornado.web.authenticated | |
| 164 | -# def get(self, topic): | |
| 165 | -# uid = self.current_user | |
| 166 | - | |
| 167 | -# try: | |
| 168 | -# ok = self.learn.start_learning(uid, topic) | |
| 169 | -# except KeyError: | |
| 170 | -# self.redirect('/') | |
| 171 | -# else: | |
| 172 | -# if ok: | |
| 173 | -# self.render('learn.html', | |
| 174 | -# uid=uid, | |
| 175 | -# name=self.learn.get_student_name(uid), | |
| 176 | -# ) | |
| 177 | -# else: | |
| 178 | -# self.redirect('/') | |
| 179 | - | |
| 180 | 161 | |
| 181 | 162 | # ---------------------------------------------------------------------------- |
| 182 | 163 | # Serves files from the /public subdir of the topics. |
| ... | ... | @@ -185,8 +166,6 @@ class TopicHandler(BaseHandler): |
| 185 | 166 | class FileHandler(BaseHandler): |
| 186 | 167 | SUPPORTED_METHODS = ['GET'] |
| 187 | 168 | |
| 188 | - # chunk_size = 4 * 1024 * 1024 # serve up to 4 MiB multiple times | |
| 189 | - | |
| 190 | 169 | @tornado.web.authenticated |
| 191 | 170 | async def get(self, filename): |
| 192 | 171 | uid = self.current_user |
| ... | ... | @@ -209,18 +188,6 @@ class FileHandler(BaseHandler): |
| 209 | 188 | self.write(f.read()) |
| 210 | 189 | await self.flush() |
| 211 | 190 | |
| 212 | - # chunk = f.read(self.chunk_size) | |
| 213 | - # while chunk: | |
| 214 | - # try: | |
| 215 | - # self.write(chunk) # write the chunk to response | |
| 216 | - # await self.flush() # flush the current chunk to socket | |
| 217 | - # except iostream.StreamClosedError: | |
| 218 | - # break # client closed the connection | |
| 219 | - # finally: | |
| 220 | - # del chunk | |
| 221 | - # await asyncio.sleep(0) # 1 nanosecond (hack) | |
| 222 | - # chunk = f.read(self.chunk_size) | |
| 223 | - | |
| 224 | 191 | |
| 225 | 192 | # ---------------------------------------------------------------------------- |
| 226 | 193 | # respond to AJAX to get a JSON question |
| ... | ... | @@ -326,6 +293,8 @@ def signal_handler(signal, frame): |
| 326 | 293 | tornado.ioloop.IOLoop.current().stop() |
| 327 | 294 | logging.critical('Webserver stopped.') |
| 328 | 295 | sys.exit(0) |
| 296 | + else: | |
| 297 | + logging.info('Abort canceled...') | |
| 329 | 298 | |
| 330 | 299 | |
| 331 | 300 | # ------------------------------------------------------------------------- |
| ... | ... | @@ -336,8 +305,10 @@ def main(): |
| 336 | 305 | argparser = argparse.ArgumentParser(description='Server for online learning. Enrolled students and topics have to be previously configured. Please read the documentation included with this software before running the server.') |
| 337 | 306 | argparser.add_argument('conffile', type=str, nargs='+', |
| 338 | 307 | help='Topics configuration file in YAML format.') # FIXME only one |
| 308 | + argparser.add_argument('--port', type=int, default=8443, | |
| 309 | + help='Port to be used by the HTTPS server') | |
| 339 | 310 | argparser.add_argument('--debug', action='store_true', |
| 340 | - help='Enable debug messages.') | |
| 311 | + help='Enable debug messages') | |
| 341 | 312 | arg = argparser.parse_args() |
| 342 | 313 | |
| 343 | 314 | # --- Setup logging |
| ... | ... | @@ -380,7 +351,8 @@ def main(): |
| 380 | 351 | logging.critical('Certificates cert.pem and privkey.pem not found') |
| 381 | 352 | sys.exit(1) |
| 382 | 353 | else: |
| 383 | - http_server.listen(8443) | |
| 354 | + http_server.listen(arg.port) | |
| 355 | + logging.info(f'Listening on port {arg.port}.') | |
| 384 | 356 | |
| 385 | 357 | # --- run webserver |
| 386 | 358 | logging.info('Webserver running... (Ctrl-C to stop)') | ... | ... |