defblack_list(string): for i in string: if i in BLACKLIST: returnTrue returnFalse
@app.route('/', methods=['GET']) defindex(): if 'username' in session: if session['username'] == 'admin': return render_template_string( "admin login success and check the secret route /secret_route_you_do_not_know") else: return render_template('hello.html', name=session['username']) else: return render_template_string("a easy flask problem,first login as the admin")
@app.route('/login', methods=['GET', 'POST']) deflogin(): if request.method == 'POST': username = request.form['username'] if 'username' in request.form else "" password = request.form['password'] if 'password' in request.form else "" if username == "" or password == "": return render_template_string("pass the username or password use get method") if username in user_dicts and user_dicts[username].password == password: session['username'] = username if username == 'admin': return render_template_string("admin login success!") else: return render_template_string("login success!!") else: return render_template_string("login fail! check /register") else: return render_template('login.html')
@app.route('/register', methods=['GET', 'POST']) defregister(): if request.method == 'POST': username = request.form['username'] if 'username' in request.form else "" password = request.form['password'] if 'password' in request.form else ""
if username == "" or password == "": return render_template_string("pass the username or password use get method")
if username notin user_dicts: user_dicts[username] = User(username, password) return render_template_string("register success") else: return render_template_string("the user already exists") else: return render_template('register.html')
@app.route('/secret_route_you_do_not_know', methods=['GET']) defsecret(): guess = request.args['guess'] if 'guess' in request.args else '' secret_num = rd.randint(0, 100000) if guess == '': return render_template_string("you should 'guess' the secret number") try: guess_num = int(guess) if guess_num == secret_num: return render_template_string('final step, check the source code') else: return render_template_string('you are wrong') except Exception: ifnot black_list(guess): return render_template_string(guess + ' error!!') else: return render_template_string('black list filter')
if __name__ == '__main__': init() app.run(host='0.0.0.0', port=80)