python一键生成验证码并部署(django)-凯发app官方网站

凯发app官方网站-凯发k8官网下载客户端中心 | | 凯发app官方网站-凯发k8官网下载客户端中心
  • 博客访问: 3600098
  • 博文数量: 365
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2522
  • 用 户 组: 普通用户
  • 注册时间: 2019-10-28 13:40
文章分类

(365)

  • (365)
文章存档

(8)

(130)

(155)

(50)

(22)

我的朋友
相关博文
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·

分类: python/ruby

2021-08-19 17:24:03

工具类代码

# coding=utf-8

import random

from io import bytesio

from pil import image,imagefont,imagedraw

def getcode():

    source = 'qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnm1234567890'

    code = ""

    for i in range(4):

        code = random.choice(source)

    return code

def set_color():

    return random.randrange(255)

def send_code_img(request):

    color_bg = (set_color(), set_color(), set_color())

    image = image.new("rgb", size=(150, 50), color=color_bg)

    imagedraw = imagedraw.draw(image, "rgb")

    font = imagefont.truetype("/static/font/alger.ttf", 55)

    yanzhen_code = getcode()

    imagedraw.text(xy=(0, 0), text=yanzhen_code, font=font, fill=(255, 255, 255))

    request.session["verify_code"]=yanzhen_code

    for i in range(800):

        # 绘制干扰点

        imagedraw.point(xy=(random.randrange(150), random.randrange(50)), fill=(set_color(), set_color(), set_color()))

    # image.show()

    fp = bytesio()

    image.save(fp,"png")

    return fp.getvalue()

if __name__=="__main__":

    pass

views的代码

from django.http import httpresponse, httpresponseredirect

from django.shortcuts import render, redirect

from django.urls import reverse

from django.views.decorators.csrf import csrf_exempt

from app.tools.yanzhen import send_code_img

def mine(request):

    return httpresponse("欢迎登录")

@csrf_exempt

def login(request):

    if request.method == "get":

        return render(request, "yanzhen.html")

    elif request.method == "post":

        post_code = request.post.get("get_code")

        verify_code = request.session.get("verify_code")

        if post_code.lower() != verify_code.lower():

            return redirect(reverse("app:login"))

        return redirect(reverse("app:mine"))

def get_code_img(request):

    code_img = send_code_img(request)

    return httpresponse(code_img, content_type='image/png')

html代码

    

    </span><span style="font-family:宋体;">验证码验证</span><span style="font-family:calibri;">

    {% load static %}

    请输入验证码:请输入验证码">

    

    

    

    

阅读(5701) | 评论(0) | 转发(0) |
0

上一篇:

下一篇:

给主人留下些什么吧!~~
")); function link(t){ var href= $(t).attr('href'); href ="?url=" encodeuricomponent(location.href); $(t).attr('href',href); //setcookie("returnouturl", location.href, 60, "/"); }
网站地图