【Python】多种方式实现生成验证码
生活随笔
收集整理的這篇文章主要介紹了
【Python】多种方式实现生成验证码
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
多種方式實現(xiàn)生成驗證碼
1.生成隨機n位驗證碼
import random def make_random_code(n):res = ''for i in range(n):s = chr(random.randint(65,90))num = str(random.randint(0,9))res += random.choice([s,num])return res print(make_random_code(6))2.生成隨機背景顏色的n位驗證碼(Django版本)
urls.py
from django.conf.urls import url from django.contrib import admin from mybbs import viewsurlpatterns = [url(r'^admin/', admin.site.urls),url(r'^login/$', views.login),url(r'^get_code/$', views.get_code), ]views.py
from django.shortcuts import * from PIL import Image,ImageDraw,ImageFont # 內(nèi)存管理,把圖片放在內(nèi)存里 from io import BytesIO import random# Create your views here.def login(request):return render(request,'login.html')def get_random_color():return (random.randint(0,255),random.randint(0,255),random.randint(0,255))def get_code(request):# 方式一# with open('./static/image/miao1.png','rb')as f :# data = f.read()# 方式二# pip3 install pillow# from PIL import Image,ImageFont,ImageDraw# Image.new('顏色類型','長寬','顏色')img = Image.new('RGB',(125,35),color=get_random_color())# 指定字體文件font = ImageFont.truetype('./static/font/kumo.ttf',30)# ImageDraw.Draw(img)draw = ImageDraw.Draw(img)valid_code = ''# 驗證碼顯示方式一:# draw.text((X軸,Y軸),'顯示內(nèi)容','顏色')# draw.text((5,5),'python',get_random_color(),font=font)# 驗證碼顯示方式二:for i in range(4):random_num = str(random.randint(0,9))random_upper = chr(random.randint(65,90))random_lower = chr(random.randint(97,122))random_chr = random.choice([random_num,random_lower,random_upper])draw.text((i*30+10,5),random_chr,get_random_color(),font=font)valid_code += random_chrprint(valid_code)# 存到session里request.session['valid_code'] = valid_code# 生成一個內(nèi)存管理對象f = BytesIO()# 將圖片保存至f對象內(nèi),指定格式為pngimg.save(f,'png')# 從內(nèi)存中取出data = f.getvalue()return HttpResponse(data) views.pylogin.htnl(存在繼承母版,自行修改)
{% extends 'base.html' %} {% block page-title %}<title>登錄</title> {% endblock %} {% block page-css %}<link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css"><link rel="stylesheet" href="/static/fontawesome/css/font-awesome.min.css"> {% endblock %} {% block page-main %}<div class="row"><div class="col-md-4" style="margin-top: 50px;"><img src="/static/image/1.png" alt=""></div><div class="col-md-6 col-md-offset-2" style="margin-top: 80px"><h2 style="padding-left: 60px;text-align: center;margin-bottom: 50px">用戶登錄</h2><form class="form-horizontal" action="#" method="post">{% csrf_token %}<div class="form-group"><label for="id_username" class="col-sm-2 control-label">賬號</label><div class="col-sm-10"><input type="text" class="form-control" id="id_username" placeholder="請輸入用戶名、郵箱、手機號"name="username"></div></div><div class="form-group"><label for="id_password" class="col-sm-2 control-label">密碼</label><div class="col-sm-10"><input type="password" class="form-control" id="id_password" placeholder="請輸入密碼"name="userpwd"></div></div><div class="form-group"><lable for="valid_code" class="col-sm-2 control-label">驗證碼</lable><div class="col-sm-7"><input type="text" class="form-control" id="valid_code"></div><div class="col-sm-3" style="padding: 0 10px"><img style="width: 125px" src="/get_code/" id="cod_img"></div></div><div class="form-group"><div class="col-sm-offset-2 col-sm-10"><button type="submit" class="btn btn-success" style="width: 457px" id="submit_btn">登錄</button></div><div class="col-sm-offset-2 col-sm-10" style="margin-top: 10px;color: red">{{ error }}</div></div></form></div></div> {% endblock %} {% block page-js %}<script src="/static/jquery-3.3.1.js"></script><script src="/static/bootstrap/js/bootstrap.min.js"></script><script>$('#cod_img').click(function () {$('#cod_img')[0].src = $('#cod_img')[0].src + '?';})</script> {% endblock %} login.html3.極客驗證
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/neymargoal/p/9672595.html
總結(jié)
以上是生活随笔為你收集整理的【Python】多种方式实现生成验证码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2018.09.18 atcoder M
- 下一篇: git cherry-pick