from flask import Flask, render_template, request, make_response, redirect from numpy import real from freecaptcha import captcha import uuid app = Flask(__name__, static_url_path='', static_folder='static',) captcha_solutions = {} captcha_solved = [] @app.route("/captcha", methods=['GET', 'POST']) def login(): # This means they just submitted a CAPTCHA # We need to see if they got it right incorrect_captcha = False if request.method == 'POST': captcha_quess = request.form.get('captcha', None) captcha_cookie = request.cookies.get('freecaptcha_cookie') real_answer = captcha_solutions.get(captcha_cookie, None) if real_answer is not None: if int(captcha_quess) == int(real_answer): captcha_solved.append(captcha_cookie) return redirect("/", code=302) else: incorrect_captcha = True # Select an image image_path = captcha.random_image() # Generate list of rotated versions of image # and save which one is correct answer, options = captcha.captchafy(image_path) # Provide the CAPTCHA options to the web page using the CAPTCHA resp = make_response(render_template("index.html", captcha_options=options, incorrect_captcha=incorrect_captcha)) # Track this user with a cookie and store the correct answer # by linking the cookie with the answer, we can check their answer # later freecaptcha_cookie = str(uuid.uuid4()) resp.set_cookie('freecaptcha_cookie', freecaptcha_cookie) captcha_solutions[freecaptcha_cookie] = answer return resp