Commit 164e22f4567deb538fd3fabfeff4c7d21437e420
1 parent
ed9d2f21
Exists in
master
and in
1 other branch
add redirect command to redirect http to https
Showing
4 changed files
with
37 additions
and
29 deletions
Show diff stats
| ... | ... | @@ -4,34 +4,41 @@ from tornado.web import RedirectHandler, Application |
| 4 | 4 | from tornado.ioloop import IOLoop |
| 5 | 5 | import argparse |
| 6 | 6 | |
| 7 | -default_url = 'https://bit.xdi.uevora.pt/' | |
| 8 | -default_port = 8080 | |
| 9 | - | |
| 10 | -# --- Commandline argument parsing | |
| 11 | -argparser = argparse.ArgumentParser( | |
| 12 | - description='Redirection server. ' | |
| 13 | - 'Listen on a given port for HTTP requests and responds with a ' | |
| 14 | - '301 - Moved Permanently - so that the browser is redirected to the ' | |
| 15 | - 'correct target address.' | |
| 16 | - ) | |
| 17 | -argparser.add_argument( | |
| 18 | - '-p', '--port', type=int, default=8080, | |
| 19 | - help='Port to listen to (default: 8080)' | |
| 20 | -) | |
| 21 | -argparser.add_argument( | |
| 22 | - '-t', '--target', type=str, default=default_url, | |
| 23 | - help='Target address (default: https://bit.xdi.uevora.pt)' | |
| 7 | +def main(): | |
| 8 | + default_url = 'https://bit.xdi.uevora.pt/' | |
| 9 | + default_port = 8080 | |
| 10 | + | |
| 11 | + # --- Commandline argument parsing | |
| 12 | + argparser = argparse.ArgumentParser( | |
| 13 | + description='Redirection server. ' | |
| 14 | + 'Listen on a given port for HTTP requests and responds with a ' | |
| 15 | + '301 - Moved Permanently - so that the browser is redirected to the ' | |
| 16 | + 'correct target address.' | |
| 17 | + ) | |
| 18 | + argparser.add_argument( | |
| 19 | + '-p', '--port', type=int, default=8080, | |
| 20 | + help='Port to listen to (default: 8080)' | |
| 24 | 21 | ) |
| 25 | -arg = argparser.parse_args() | |
| 26 | -print(f'Redirecting from port {arg.port} to {arg.target}') | |
| 22 | + argparser.add_argument( | |
| 23 | + '-t', '--target', type=str, default=default_url, | |
| 24 | + help='Target address (default: https://bit.xdi.uevora.pt)' | |
| 25 | + ) | |
| 26 | + arg = argparser.parse_args() | |
| 27 | + print(f'Redirecting from port {arg.port} to {arg.target}') | |
| 28 | + | |
| 29 | + # --- run server | |
| 30 | + handlers = [(r'.*', RedirectHandler, {'url': arg.target})] | |
| 31 | + app = Application(handlers) | |
| 32 | + app.listen(arg.port) | |
| 33 | + | |
| 34 | + try: | |
| 35 | + IOLoop.current().start() # running... | |
| 36 | + except Exception: | |
| 37 | + IOLoop.current().stop() | |
| 27 | 38 | |
| 28 | -handlers = [(r'.*', RedirectHandler, {'url': arg.target})] | |
| 29 | -app = Application(handlers) | |
| 30 | -app.listen(arg.port) | |
| 39 | + print('Redirection stopped!') | |
| 31 | 40 | |
| 32 | -try: | |
| 33 | - IOLoop.current().start() # running... | |
| 34 | -except Exception: | |
| 35 | - IOLoop.current().stop() | |
| 36 | 41 | |
| 37 | -print('Redirection stopped!') | |
| 42 | +# ---------------------------------------------------------------------------- | |
| 43 | +if __name__ == "__main__": | |
| 44 | + main() | ... | ... |
aprendizations/serve.py
| ... | ... | @@ -397,8 +397,8 @@ def get_logger_config(debug=False): |
| 397 | 397 | 'version': 1, |
| 398 | 398 | 'formatters': { |
| 399 | 399 | 'standard': { |
| 400 | - 'format': '%(asctime)s %(name)-22s %(levelname)-8s %(message)s', | |
| 401 | - 'datefmt': '%H:%M', | |
| 400 | + 'format': '%(asctime)s %(name)-24s %(levelname)-8s %(message)s', | |
| 401 | + 'datefmt': '%H:%M:%S', | |
| 402 | 402 | }, |
| 403 | 403 | }, |
| 404 | 404 | 'handlers': { | ... | ... |
setup.py