Flask 驗(yàn)證碼自動(dòng)生成的實(shí)現(xiàn)示例
想必驗(yàn)證碼大家都有所了解,但是可以自己定義圖片驗(yàn)證碼,包含數(shù)字,英文以及數(shù)字計(jì)算,自動(dòng)生成驗(yàn)證碼。
生成圖片以及結(jié)果
from captcha.image import ImageCaptcha from PIL import Image from random import choices def gen_captcha(content='2345689abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ'): """ 生成驗(yàn)證碼,可自定義,這里是生成驗(yàn)證碼結(jié)果,以及驗(yàn)證碼文件 """ image = ImageCaptcha() # 獲取字符串 captcha_text = "".join(choices(content, k=4)).lower() # 生成圖像 captcha_image = Image.open(image.generate(captcha_text)) return captcha_text, captcha_image
得到生成的驗(yàn)證碼,進(jìn)行處理,響應(yīng)到頁(yè)面
處理驗(yàn)證碼
from io import BytesIO from flask import make_response,session # 生成驗(yàn)證碼 def get_captcha(): code, image = gen_captcha() #得到驗(yàn)證碼內(nèi)容 out = BytesIO() # 操作二進(jìn)制數(shù)據(jù),實(shí)例化 image.save(out, 'png') # 保存驗(yàn)證碼圖片 out.seek(0) resp = make_response(out.read()) #將驗(yàn)證碼圖片的bytes制作成頁(yè)面響應(yīng)結(jié)果, 具體可查看make_response這個(gè)方法 resp.content_type = 'image/png' # 告訴頁(yè)面這個(gè)響應(yīng)的類型 return resp, code
制作響應(yīng)路由,使用藍(lán)圖
藍(lán)圖
from flask import Blueprint, session, redirect, url_for, render_template, request passport_bp = Blueprint('passport', __name__, url_prefix='/passport') # 生成藍(lán)圖passport_bp # 獲取驗(yàn)證碼 @passport_bp.get('/getCaptcha') def get_captcha(): resp, code = index_curd.get_captcha() #獲取圖片 session["code"] = code # 驗(yàn)證碼結(jié)果保存到session或者數(shù)據(jù)庫(kù)中,這里保存在session return resp # 返回圖片結(jié)果
html頁(yè)面展示
html展示
""" <img src="{{ url_for('passport.get_captcha') }}" class="codeImage" id="captchaImage"/> """ <script> # 使用的layui框架 layui.use(['form', 'jquery', 'layer', 'button', 'popup'], function () { let form = layui.form; let $ = layui.jquery; let layer = layui.layer; let button = layui.button; let popup = layui.popup; let captchaPath = "{{ url_for('passport.get_captcha') }}"; $("#captchaImage").click(function () { # 點(diǎn)擊切換 document.getElementById("captchaImage").src = captchaPath + "?" + Math.random(); }); setInterval(function () { # 30秒自動(dòng)切換 document.getElementById("captchaImage").src = captchaPath + "?" + Math.random(); }, 30 * 1000); }) </script>
到此這篇關(guān)于Flask 驗(yàn)證碼自動(dòng)生成的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Flask 驗(yàn)證碼自動(dòng)生成內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python + Flask 實(shí)現(xiàn)簡(jiǎn)單的驗(yàn)證碼系統(tǒng)
- python web框架Flask實(shí)現(xiàn)圖形驗(yàn)證碼及驗(yàn)證碼的動(dòng)態(tài)刷新實(shí)例
- flask實(shí)現(xiàn)驗(yàn)證碼并驗(yàn)證功能
- Flask項(xiàng)目中實(shí)現(xiàn)短信驗(yàn)證碼和郵箱驗(yàn)證碼功能
- Python Flask實(shí)現(xiàn)圖片驗(yàn)證碼與郵箱驗(yàn)證碼流程詳細(xì)講解
- 基于Python+Flask實(shí)現(xiàn)一個(gè)簡(jiǎn)易網(wǎng)頁(yè)驗(yàn)證碼登錄系統(tǒng)案例
- Python的Flask框架開(kāi)發(fā)驗(yàn)證碼登錄的實(shí)現(xiàn)
相關(guān)文章
解決遇到PermissionError:[Errno 13] Permission den
遇到"PermissionError:[Errno 13] Permission denied"通常是權(quán)限不足導(dǎo)致,解決此問(wèn)題的方法包括檢查并更改文件權(quán)限,使用管理員權(quán)限運(yùn)行命令,或接觸文件所有者,這些步驟有助于確保用戶具有執(zhí)行操作所需的權(quán)限,有時(shí),文件或目錄可能被鎖定2024-09-09python opencv將圖片轉(zhuǎn)為灰度圖的方法示例
這篇文章主要介紹了python opencv將圖片轉(zhuǎn)為灰度圖的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07Tensorflow tensor 數(shù)學(xué)運(yùn)算和邏輯運(yùn)算方式
這篇文章主要介紹了Tensorflow tensor 數(shù)學(xué)運(yùn)算和邏輯運(yùn)算方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06Python3使用requests模塊實(shí)現(xiàn)顯示下載進(jìn)度的方法詳解
這篇文章主要介紹了Python3使用requests模塊實(shí)現(xiàn)顯示下載進(jìn)度的方法,結(jié)合實(shí)例形式分析了Python3中requests模塊的配置、使用及顯示進(jìn)度條類的相關(guān)定義方法,需要的朋友可以參考下2019-02-02pandas 對(duì)group進(jìn)行聚合的例子
今天小編就為大家分享一篇pandas 對(duì)group進(jìn)行聚合的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12Python文件簡(jiǎn)單操作及openpyxl操作excel文件詳解
這篇文章主要為大家詳細(xì)介紹了python對(duì)文件的簡(jiǎn)單使用及openpyxl操作excel文件的方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-10-10python opencv實(shí)現(xiàn)圖片旋轉(zhuǎn)矩形分割
這篇文章主要為大家詳細(xì)介紹了python opencv實(shí)現(xiàn)圖片旋轉(zhuǎn)矩形分割,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07