前后端分离实现一个完整的网页验证码流程,前端使用flask框架,先在python中做一个画布,在画布上实现带干扰线与噪声点的指定问题呈现,问题与答案相对应作为后端在前端请求时显示问题,问题是谁最帅,谁最丑,谁最高,答案是我最帅,我最丑,我最高,将画布呈现在前端的HTML代码,问题与答案相对应,同时会实现随机问题的呈现,在前端HTML中设置看不清换一张按钮,点击可以更改画布图片,用户需要通过语音输入的方式输入答案,通过按钮开始输入开始进行语音输入,通过结束按钮结束语音输入,不允许用户通过文本方式输入答案,python后端与html前端可以实现交互的实现的前端完整代码与后端完整代码实现
时间: 2023-07-15 15:11:14 浏览: 120
python web框架Flask实现图形验证码及验证码的动态刷新实例
前端代码:
```HTML
<!DOCTYPE html>
<html>
<head>
<title>验证码</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container mt-5">
<h1 class="text-center mb-5">验证码</h1>
<div class="row justify-content-center">
<div class="col-md-6">
<img id="captcha" src="{{ url_for('captcha') }}" alt="验证码" class="img-fluid mb-3">
<button id="change-captcha" class="btn btn-primary mb-3">看不清,换一张</button>
<form>
<div class="form-group">
<label for="answer">请输入答案</label>
<input type="text" class="form-control" id="answer" autocomplete="off">
</div>
<div class="form-group">
<button id="start-record-btn" class="btn btn-primary">开始录音</button>
<button id="stop-record-btn" class="btn btn-danger" disabled>结束录音</button>
</div>
<button type="submit" class="btn btn-primary" id="submit-btn" disabled>提交</button>
</form>
</div>
</div>
</div>
<script>
var recognition;
window.onload = function() {
var changeCaptchaBtn = document.getElementById('change-captcha');
changeCaptchaBtn.onclick = function() {
document.getElementById('captcha').src = "{{ url_for('captcha') }}?t=" + Math.random();
document.getElementById('answer').value = "";
document.getElementById('submit-btn').disabled = true;
}
var startRecordBtn = document.getElementById('start-record-btn');
var stopRecordBtn = document.getElementById('stop-record-btn');
startRecordBtn.onclick = function() {
startRecordBtn.disabled = true;
stopRecordBtn.disabled = false;
recognition = new webkitSpeechRecognition();
recognition.lang = "zh-CN";
recognition.start();
}
stopRecordBtn.onclick = function() {
startRecordBtn.disabled = false;
stopRecordBtn.disabled = true;
recognition.stop();
}
recognition.onresult = function(event) {
var result = event.results[0][0].transcript;
document.getElementById('answer').value = result;
document.getElementById('submit-btn').disabled = false;
}
}
</script>
</body>
</html>
```
后端代码:
```python
from flask import Flask, render_template, request, make_response
from io import BytesIO
from PIL import Image, ImageDraw, ImageFont
import random
app = Flask(__name__)
questions = {
"谁最帅": "我最帅",
"谁最丑": "我最丑",
"谁最高": "我最高"
}
def generate_captcha():
background_color = (255, 255, 255)
font_size = 25
font_color = (0, 0, 0)
font = ImageFont.truetype("arial.ttf", font_size)
width = 120
height = 50
image = Image.new("RGB", (width, height), background_color)
draw = ImageDraw.Draw(image)
for x in range(width):
for y in range(height):
if random.random() > 0.8:
draw.point((x, y), fill=(0, 0, 0))
for i in range(4):
char = random.choice("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
draw.text((10+i*25, 15), char, font=font, fill=font_color)
captcha = BytesIO()
image.save(captcha, "png")
captcha.seek(0)
return captcha
@app.route("/")
def index():
return render_template("index.html")
@app.route("/captcha")
def captcha():
captcha = generate_captcha()
response = make_response(captcha.getvalue())
response.headers["Content-Type"] = "image/png"
return response
@app.route("/check", methods=["POST"])
def check():
answer = request.form.get("answer")
question = request.form.get("question")
if answer == questions[question]:
return "验证成功"
else:
return "验证失败"
if __name__ == "__main__":
app.run(debug=True)
```
注意:以上代码仅供参考,实际应用中需要根据具体情况进行修改。
阅读全文