基于Python+Flask實現(xiàn)一個簡易網頁驗證碼登錄系統(tǒng)案例
1. 開始之前
首先,確保你已經安裝了以下所需的庫:
pip install flask Pillow
- Flask: 一個輕量級的Web服務器和框架。
- Pillow: 處理圖像操作,用于生成驗證碼圖像。
2. 生成驗證碼圖像
我們使用Pillow庫來生成驗證碼圖像。除了顯示數(shù)字和字母,為了增加安全性,我們還會在圖像上添加一些干擾線條和噪點。
from PIL import Image, ImageDraw, ImageFont
import random
import string
def generate_captcha_image():
# 定義圖片大小及背景顏色
image = Image.new('RGB', (120, 30), color=(73, 109, 137))
# 使用系統(tǒng)自帶字體,或指定字體文件路徑
font_path = "./arial.ttf"
fnt = ImageFont.truetype(font_path, 15)
d = ImageDraw.Draw(image)
# 生成5位數(shù)的驗證碼文本
captcha_text = ''.join(random.choices(string.ascii_uppercase + string.digits, k=5))
d.text((10, 10), captcha_text, font=fnt, fill=(255, 255, 0))
# 添加干擾線條和噪點
for _ in range(random.randint(3, 5)):
start = (random.randint(0, image.width), random.randint(0, image.height))
end = (random.randint(0, image.width), random.randint(0, image.height))
d.line([start, end], fill=(random.randint(50, 200), random.randint(50, 200), random.randint(50, 200)))
for _ in range(100):
xy = (random.randrange(0, image.width), random.randrange(0, image.height))
d.point(xy, fill=(random.randint(50, 200), random.randint(50, 200), random.randint(50, 200)))
return image, captcha_text3. 使用Flask建立Web應用
現(xiàn)在,我們使用Flask來創(chuàng)建一個Web應用,并展示登錄頁面與驗證碼圖像。
from flask import Flask, render_template, jsonify, request, session
import io
app = Flask(__name__)
app.config['SECRET_KEY'] = 'your_secret_key_here'
@app.route('/')
def index():
# 渲染登錄頁面
return render_template('login.html')
@app.route('/captcha')
def captcha():
# 使用上述函數(shù)生成驗證碼圖片
image, captcha_text = generate_captcha_image()
# 將驗證碼文本存儲到session,以便之后進行驗證
session['captcha'] = captcha_text
buf = io.BytesIO()
image.save(buf, format='PNG')
buf.seek(0)
return buf.getvalue(), 200, {
'Content-Type': 'image/png',
'Content-Length': str(len(buf.getvalue()))
}4. 處理登錄請求
登錄時,我們需要驗證用戶輸入的驗證碼是否與我們生成的匹配。
@app.route('/login', methods=['POST'])
def login():
# 檢查用戶輸入的驗證碼是否與session中的一致
if request.json.get('captcha', '').upper() == session.get('captcha', '').upper():
return jsonify({'status': 'success', 'message': '登錄成功'})
else:
return jsonify({'status': 'error', 'message': '驗證碼錯誤'}), 4005. 總結
通過上面的代碼,我們創(chuàng)建了一個簡單的網站驗證碼登錄系統(tǒng)。用戶需要輸入與圖片上顯示的驗證碼匹配的文本來驗證自己是人類。這不僅提高了安全性,而且能夠有效地阻止惡意機器人。 盡管此示例只是基礎版本,但您可以在此基礎上添加更多的安全性措施,例如使用更復雜的驗證碼、添加限制登錄嘗試次數(shù)的功能或使用其他驗證方法。 希望本文能幫助您了解如何使用Python和Flask來創(chuàng)建驗證碼登錄系統(tǒng)。在實際開發(fā)中,為了提供更好的用戶體驗和安全性,建議進一步完善和增強此系統(tǒng)。
以上就是基于Python+Flask實現(xiàn)一個簡易網頁驗證碼登錄系統(tǒng)案例的詳細內容,更多關于Python+Flask網頁驗證碼登錄的資料請關注腳本之家其它相關文章!
相關文章
使用Python實現(xiàn)寫入多類型數(shù)據(jù)至Excel文件
Python 憑借其豐富的生態(tài)系統(tǒng),在辦公自動化領域展現(xiàn)出顯著優(yōu)勢,本文將介紹如何使用 Free Spire.XLS for Python 庫,以程序化方式高效、可靠地將多種數(shù)據(jù)類型寫入 Excel 文件,希望對大家有所幫助2025-12-12
合并百度影音的離線數(shù)據(jù)( with python 2.3)
這篇文章主要介紹了合并百度影音的離線數(shù)據(jù)( with python 2.3)的相關資料2015-08-08
Python使用pyttsx3庫實現(xiàn)離線文字轉語音功能
文章介紹了pyttsx3庫,這是一個用于Python的離線文本轉語音庫,支持跨平臺使用,它能夠實現(xiàn)基礎文本轉語音、自定義語速和音量、切換語音類型、中斷語音、批量朗讀文件和將語音保存為音頻文件等功能,需要的朋友可以參考下2026-01-01
Pytorch dataloader在加載最后一個batch時卡死的解決
這篇文章主要介紹了Pytorch dataloader在加載最后一個batch時卡死的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05

