#! /bin/python3 from random import choice from flask import Flask, render_template, request from configparser import ConfigParser from datetime import datetime import random import logging import pytz app = Flask(__name__, static_url_path='', static_folder='static', template_folder='templates') config = ConfigParser() config.read('config.ini') logging.basicConfig(encoding='utf-8', level=logging.INFO, format='%(message)s') logger = logging.getLogger('waitress') tz = pytz.timezone(config.get('main', 'timezone')) @app.after_request def log_the_request(response): now = datetime.now(tz=tz) if 'X-Forwarded-For' in request.headers: remote_addr = request.headers['X-Forwarded-For'] else: remote_addr = request.remote_addr if not request.remote_user: remote_user = "-" else: remote_user = request.remote_user if not request.referrer: referrer = "-" else: referrer = request.referrer if request.full_path[-1] == '?': full_path = request.full_path[:-1] else: full_path = request.full_path log = { 'remote_addr': remote_addr, 'remote_user': remote_user, 'url': full_path, 'date': now.strftime("%d/%b/%Y:%H:%M:%S %z"), 'referrer': referrer, 'user_agent': request.user_agent.string, 'method': request.method, 'content_length': response.content_length, 'status_code': response.status_code } logfile = "{} - {} [{}] \"{} {}\" {} {} \"{}\" \"{}\"".format(log['remote_addr'], log['remote_user'], log['date'], log['method'], log['url'], log['status_code'], log['content_length'], log['referrer'], log['user_agent']) logger.info(logfile) return response @app.errorhandler(404) def page_not_found(e): lang = config.get('i18n', 'lang') title = config.get('i18n', 'title') url = config.get('main', 'base_url') theme = config.get('main', 'theme') desc = config.get('i18n', 'desc') return render_template('404.html', title=title, lang=lang, desc=desc, theme=theme, url=url), 404 @app.errorhandler(500) def internal_server_error(e): lang = config.get('i18n', 'lang') title = config.get('i18n', 'title') url = config.get('main', 'base_url') theme = config.get('main', 'theme') desc = config.get('i18n', 'desc') return render_template('500.html', title=title, lang=lang, desc=desc, theme=theme, url=url), 500 @app.route("/") def hello(): lang = config.get('i18n', 'lang') title = config.get('i18n', 'title') more = config.get('i18n', 'more') desc = config.get('i18n', 'desc') questions_prefix = config.get('i18n', 'questions_prefix') questions_suffix = config.get('i18n', 'questions_suffix') send_questions = config.get('i18n', 'send_questions') separator = config.get('i18n', 'separator') separator_char = config.get('main', 'separator_char') mail = config.get('main', 'mail') mailtext = config.get('i18n', 'mailtext') help = config.get('i18n', 'help_title') help1 = config.get('i18n', 'help_1') help2 = config.get('i18n', 'help_2') help3 = config.get('i18n', 'help_3') url = config.get('main', 'base_url') theme = config.get('main', 'theme') ablines = [] now = datetime.now(tz=tz) epoch = now.timestamp() epoch = int(epoch) lines = getContent() while len(lines) < 2: logger.error('Error reading content') print(lines) lines = getContent() for line in lines: ab = line.split(separator_char) ablines.append( {'A': str(ab[0]), 'B': str(ab[1])} ) with open("ab.txt", "r") as f: num_lines = sum(1 for _ in f) return render_template('index.html', title=title, separator=separator, content=ablines, num_lines=num_lines, epoch=epoch, mailto=mail, more=more, questions_prefix=questions_prefix, questions_suffix=questions_suffix, send_questions=send_questions, lang=lang, url=url, desc=desc, theme=theme, mailtext=mailtext, help=help, help_1=help1, help_2=help2, help_3=help3) def getContent(): lines = [a.strip() for a in open("ab.txt", "r").readlines()] result = random.sample(lines, 5) return result app.register_error_handler(404, page_not_found) app.register_error_handler(500, internal_server_error) if __name__ == "__main__": from waitress import serve bind = config.get('main', 'bind') port = config.get('main', 'port') serve(app, host=bind, port=port, ident='a/b game')