用户登录验证码生成

Posted zbj666

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用户登录验证码生成相关的知识,希望对你有一定的参考价值。

一、视图代码
import random


def get_random_color():
    """
    获取随机图片颜色
    :return: 
    """
    return random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)


def valid_img(request):
    # 方式一
    # with open("111.png", "rb") as f:
    #     data = f.read()

    # 方式二
    # from PIL import Image
    # img = Image.new("RGB", (250, 40), get_random_color())
    # f = open("validcode.png", "wb")
    # img.save(f, "png")
    # with open("validcode.png", "rb") as f:
    #     data = f.read()

    # 方式三
    # from io import BytesIO
    # img = Image.new("RGB", (250, 40), get_random_color())
    # f=BytesIO()
    # img.save(f, "png")
    # data = f.getvalue()

    # 方式四
    from io import BytesIO
    from PIL import Image, ImageDraw, ImageFont
    img = Image.new("RGB", (250, 40), get_random_color())  # 新建图片大小为250*40
    draw = ImageDraw.Draw(img)  # 可以在该图片对象上写内容
    font = ImageFont.truetype("statics/font/KaushanScript-Regular.ttf", 30)  # 指定字体,需自行下载字体文件

    keep_str = ""
    for i in range(5):  # 获取随机数
        random_num = str(random.randint(0, 9))
        random_low_alpha = chr(random.randint(97, 122))
        random_upper_alpha = chr(random.randint(65, 90))
        random_char = random.choice([random_num, random_low_alpha, random_upper_alpha])
        draw.text((20 + i * 35, 0), random_char, get_random_color(), font=font)
        keep_str += random_char

    # 噪点噪线
    # width=250
    # height=40
    # for i in range(10):
    #     x1=random.randint(0,width)
    #     x2=random.randint(0,width)
    #     y1=random.randint(0,height)
    #     y2=random.randint(0,height)
    #     draw.line((x1,y1,x2,y2),fill=get_random_color())
    #
    # for i in range(100):
    #     draw.point([random.randint(0, width), random.randint(0, height)], fill=get_random_color())
    #     x = random.randint(0, width)
    #     y = random.randint(0, height)
    #     draw.arc((x, y, x + 4, y + 4), 0, 90, fill=get_random_color())
    request.session["keep_str"] = keep_str
    f = BytesIO()
    img.save(f, "png")
    data = f.getvalue()

    return HttpResponse(data)
二、模板文件代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="/static/plugins/bootstrap-3.3.7-dist/css/bootstrap.css">
    <style>
        .con{
            margin-top: 120px;
        }
    </style>
</head>
<body>

<div class="container">
    <div class="row con">
        <form action="/login/" method="post">
            {% csrf_token %}
            <div class="form-group col-sm-6 col-sm-offset-3">
                <label for="exampleInputEmail1">用户名</label>
                <input type="text" class="form-control" id="exampleInputEmail1" placeholder="用户名">
            </div>
            <div class="form-group col-sm-6 col-sm-offset-3">
                <label for="exampleInputPassword1">密码</label>
                <input type="password" class="form-control" id="exampleInputPassword1" placeholder="密码">
            </div>
            <div class="form-group col-sm-6 col-sm-offset-3">
                <label for="exampleInputFile">验证码</label>
                <div class="row">
                    <div class="col-sm-6">
                        <input type="text" id="exampleInputFile" class="form-control col-sm-3">
                    </div>
                    <div class="col-sm-4">
{#                        <img src="/static/images/111.png" alt="">#}
                        <img src="/valid_img/" alt="">
                    </div>
                </div>
            </div>
            <div class="form-group col-sm-6 col-sm-offset-3">
                <button type="submit" class="btn btn-defaultcol-sm-offset-3">登录</button>
            </div>

        </form>

    </div>
</div>

</body>
</html>

以上是关于用户登录验证码生成的主要内容,如果未能解决你的问题,请参考以下文章

3-爬虫-模拟登录图片验证码处理

简易登录表单的制作,包括用户名密码随机验证码(代码完整,复制即用)

微信小程序中短信验证码登录全流程及代码

javaweb中如何实时判定验证码的正误

注册/登陆界面验证码的作用及代码实现

jsp使用servlet实现用户登录 及动态验证码