当前位置: 首页 > news >正文

农产品网站建设背景wordpress 二次验证

农产品网站建设背景,wordpress 二次验证,深圳宝安做网站,陕西网站制作公司哪家好验证码 在用户注册、登录页面#xff0c;为了防止暴力请求#xff0c;可以加入验证码功能#xff0c;如果验证码错误#xff0c;则不需要继续处理#xff0c;可以减轻业务服务器、数据库服务器的压力。 1#xff09;安装包Pillow3.4.1。 1pip install Pillow3.4.1点击查看… 验证码 在用户注册、登录页面为了防止暴力请求可以加入验证码功能如果验证码错误则不需要继续处理可以减轻业务服务器、数据库服务器的压力。 1安装包Pillow3.4.1。 1 pip install Pillow3.4.1 点击查看PIL模块APIhttp://pillow.readthedocs.io/en/3.4.x/以下代码中用到了Image、ImageDraw、ImageFont对象及方法。 2在booktest/views.py文件中创建视图verify_code。 提示1随机生成字符串后存入session中用于后续判断。提示2视图返回mime-type为image/png。 def verify_code(request):#引入随机函数模块import random#定义变量用于画面的背景色、宽、高bgcolor (random.randrange(20, 100), random.randrange(20, 100), 255)width 100height 25#创建画面对象im Image.new(RGB, (width, height), bgcolor)#创建画笔对象draw ImageDraw.Draw(im)#调用画笔的point()函数绘制噪点for i in range(0, 100):xy (random.randrange(0, width), random.randrange(0, height))fill (random.randrange(0, 255), 255, random.randrange(0, 255))draw.point(xy, fillfill)#定义验证码的备选值str1 ABCD123EFGHIJK456LMNOPQRS789TUVWXYZ0#随机选取4个值作为验证码rand_str for i in range(0, 4):rand_str str1[random.randrange(0, len(str1))]#构造字体对象ubuntu的字体路径为“/usr/share/fonts/truetype/freefont”font ImageFont.truetype(FreeMono.ttf, 23)#构造字体颜色fontcolor (255, random.randrange(0, 255), random.randrange(0, 255))#绘制4个字draw.text((5, 2), rand_str[0], fontfont, fillfontcolor)draw.text((25, 2), rand_str[1], fontfont, fillfontcolor)draw.text((50, 2), rand_str[2], fontfont, fillfontcolor)draw.text((75, 2), rand_str[3], fontfont, fillfontcolor)#释放画笔del draw#存入session用于做进一步验证request.session[verifycode] rand_strpython2的为# 内存文件操作import cStringIObuf cStringIO.StringIO()# 内存文件操作--此方法为python3的import iobuf io.BytesIO()#将图片保存在内存中文件类型为pngim.save(buf, png)#将内存中的图片数据返回给客户端MIME类型为图片pngreturn HttpResponse(buf.getvalue(), image/png) 3打开booktest/urls.py文件配置url。 1 url(r^verify_code/$, views.verify_code), 4运行服务器在浏览器中输入如下网址即可看到。 1 http://127.0.0.1:8000/verify_code/ 利用以上画布代码生成__看不请换一张__效果 打开templates/booktest文件创建show_verify2.html,编写代码如下 !DOCTYPE html html langen headmeta charsetUTF-8titleshow_verify/title #需要导入js文件script src/static/js/jquery-1.12.4.min.js/scriptscript$(function () {// 添加点击事件 鼠标浮动时变成小手$(#change).css(cursor, pointer).click(function () {// 获取到图片的src路径 换一个新的路径 此代码相当与在原来的基础上增加数据$(#yzm).attr(src, $(#yzm).attr(src)?1)})})/script /head body form action/verify_check2/ methodpost{% csrf_token %}请输入验证码input typetext nameverifybr{# 方法一#} {#    img idyzm src/verify_code//#} {#    span idchange#} {#    a href/verify_show/ styletext-decoration:none看不清楚换一个/a#} {#    /span#} {#    hr#} {#方法二#} img idyzm src/verify_code/?1/ span idchangea href styletext-decoration:none看不清换一个/a /span input typesubmit value提交 /form /body /html def verify_check2(request): 验证码的验证# 1.获取post请求当中的输入验证码的内容verify request.POST.get(verify)# 2.获取浏览器请求当中的session中的值verifycode request.session.get(verifycode)# 3.判断两个验证码是否相同if verify verifycode:return HttpResponse(ok)else:return HttpResponse(err)def show_verify2(request):显示验证码界面return render(request, booktest/show_verify2.html) url(r^verify_code/$, views.verify_code), # 配置验证码图片url(r^show_verify2/$, views.show_verify2), # 显示验证码界面url(r^verify_check2/$, views.verify_check2) # 检测验证码 总结以上就是画布生成的验证码保存到session中发给浏览器当浏览器提交验证时将用户输入的验证码及session中的验证码对比即可
http://wiki.neutronadmin.com/news/1539/

相关文章: