FreeCAPTCHA/app.py

55 lines
1.9 KiB
Python
Raw Normal View History

2022-04-11 23:44:17 -04:00
from flask import Flask, render_template, request, make_response, redirect
from numpy import real
from freecaptcha import captcha
import uuid
2023-06-12 09:56:17 -04:00
import os
2022-04-11 23:44:17 -04:00
app = Flask(__name__, static_url_path='', static_folder='static',)
captcha_solutions = {}
captcha_solved = []
2023-06-12 09:56:17 -04:00
@app.route("/captcha2", methods=['GET'])
def captcha2():
with open('templates/captcha2play.html') as play_template_file:
return play_template_file.read()
@app.route("/captcha3", methods=['GET'])
def captcha3():
return captcha.generate_captcha_html(os.listdir('static/images/'))
2022-04-11 23:44:17 -04:00
@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