๐Ÿ‘จ๐Ÿผ‍๐Ÿ’ป๊ฐœ๋ฐœ/Flask

Flask - ํŒŒ์ด์ฌ flask ๊ฐ„๋‹จํ•œ ์บก์ฑ  ์˜ˆ์ œ(flask-simple-captcha)

Janger 2023. 2. 2. 23:40
728x90

 

๋ชจ๋“ˆ ์„ค์น˜

 

pip install flask-simple-captcha

 

 

simple_captcha_example.py

 

from flask import Flask, render_template, request
from flask_simple_captcha import CAPTCHA

config = {
    'SECRET_CAPTCHA_KEY': '1111111111111111111111111',
    'METHOD': 'pbkdf2:sha256:100',
    'CAPTCHA_LENGTH': 5,
    'CAPTCHA_DIGITS': False
}


@app.route('/example', methods=['GET','POST'])
def example():
    if request.method == 'GET':
          captcha = CAPTCHA.create()
          return render_template('example.html', captcha=captcha)
    if request.method == 'POST':
        c_hash = request.form.get('captcha-hash')
        c_text = request.form.get('captcha-text')
        if CAPTCHA.verify(c_text, c_hash):
            return 'success'
        else:
            return 'failed captcha'

 

example.html

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>simple captcha example</title>
</head>
<body>

    <form action="/example" method="post">

        <img src={{ "data:image/png;base64," + captcha['img'] }} alt="">
        <input type="hidden" name="captcha-hash" value={{ captcha['hash'] }}>
        <input type="text" name="captcha-text">
        
        <button type="submit">Submit</button>

    </form>
   
</body>
</html>

 

 

๊ฒฐ๊ณผ

 

 

 

 

 

์ถœ์ฒ˜ ๋ฐ ์ฐธ๊ณ 

 

https://pypi.org/project/flask-simple-captcha/

 

flask-simple-captcha

Extremely simple, "Good Enough" captcha implemention for flask forms. No server side sessions required.

pypi.org

 

https://github.com/cc-d/flask-simple-captcha

 

728x90