微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

随机验证码

<div class="postBody">
<div id="cnblogs_post_body">

Python生成随机验证码,需要使用PIL模块.python3则是pillow

安装:

Highlighter sh-gutter">
Highlighter_487059" class="SyntaxHighlighter python">

基本使用

1. 创建图片

2. 创建画笔,用于在图片上画任意内容

Highlighter sh-gutter">
Highlighter_251344" class="SyntaxHighlighter python">

3. 画点

一个参数:表示坐标

4. 画线

Highlighter sh-gutter">
Highlighter_374804" class="SyntaxHighlighter python">
一个参数:表示起始坐标和结束坐标(圆要画在其中间)

6. 写文本

Highlighter sh-gutter">
Highlighter_736754" class="SyntaxHighlighter python">
一个参数:表示起始坐标
内容

图片验证

Highlighter sh-gutter">
Highlighter_257991" class="SyntaxHighlighter python">

  

  

 

PIL io img </span>= Image.new(mode=<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;RGB</span><span style="color: #800000;"&gt;'</span>,size=(120,40<span style="color: #000000;"&gt;),color</span>=(random.randint(0,255),random.randint(0,255<span style="color: #000000;"&gt;))) draw </span>= ImageDraw.Draw(img,<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;RGB</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;) font </span>= ImageFont.truetype(<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;app01/static/font/kumo.ttf</span><span style="color: #800000;"&gt;'</span>,25<span style="color: #000000;"&gt;) valid_list </span>=<span style="color: #000000;"&gt; [] </span><span style="color: #0000ff;"&gt;for</span> i <span style="color: #0000ff;"&gt;in</span> range(5<span style="color: #000000;"&gt;): random_num </span>= str(random.randint(0,9<span style="color: #000000;"&gt;)) random_upper_alp </span>= chr(random.randint(65,90<span style="color: #000000;"&gt;)) random_lower_alp </span>= chr(random.randint(97,122<span style="color: #000000;"&gt;)) valid_ele </span>=<span style="color: #000000;"&gt; random.choice([random_num,random_upper_alp,random_lower_alp]) valid_list.append(valid_ele) draw.text([</span>5 + i * 24,10],valid_ele,(random.randint(0,255<span style="color: #000000;"&gt;)),font</span>=<span style="color: #000000;"&gt;font) </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 写干扰点</span> <span style="color: #0000ff;"&gt;for</span> i <span style="color: #0000ff;"&gt;in</span> range(40<span style="color: #000000;"&gt;): draw.point([random.randint(0,</span>120),40<span style="color: #000000;"&gt;)],fill</span>=(random.randint(0,random.randint(10,random.randint(64,255<span style="color: #000000;"&gt;))) </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 写干扰圆圈</span> <span style="color: #0000ff;"&gt;for</span> i <span style="color: #0000ff;"&gt;in</span> range(40<span style="color: #000000;"&gt;): draw.point([random.randint(0,255<span style="color: #000000;"&gt;))) x </span>= random.randint(0,120<span style="color: #000000;"&gt;) y </span>= random.randint(0,40<span style="color: #000000;"&gt;) draw.arc((x,x </span>+ 4,y + 4),90<span style="color: #000000;"&gt;,255<span style="color: #000000;"&gt;))) </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 画干扰线</span> <span style="color: #0000ff;"&gt;for</span> i <span style="color: #0000ff;"&gt;in</span> range(5<span style="color: #000000;"&gt;): x1 </span>= random.randint(0,120<span style="color: #000000;"&gt;) y1 </span>= random.randint(0,40<span style="color: #000000;"&gt;) x2 </span>= random.randint(0,120<span style="color: #000000;"&gt;) y2 </span>= random.randint(0,40<span style="color: #000000;"&gt;) draw.line((x1,255<span style="color: #000000;"&gt;))) f </span>=<span style="color: #000000;"&gt; BytesIO() img.save(f,</span><span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;png</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;) data </span>=<span style="color: #000000;"&gt; f.getvalue() valid_str </span>= <span style="color: #800000;"&gt;''</span>.join(valid_list) <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;将验证码拼接成字符串</span> <span style="color: #0000ff;"&gt;print</span><span style="color: #000000;"&gt;(valid_str) request.session[</span><span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;keepValidCode</span><span style="color: #800000;"&gt;"</span>] = valid_str <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;将验证码保存在session中,方便在登陆时验证</span> <span style="color: #0000ff;"&gt;return</span> HttpResponse(data)</pre>

app01 django.contrib.auth request.method == = render(request,,{== LoginForm(data== request.POST.get( </span><span style="color: #0000ff;"&gt;if</span> <span style="color: #0000ff;"&gt;not</span><span style="color: #000000;"&gt; form.is_valid(): </span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; HttpResponse(json.dumps(dict(form.errors))) </span><span style="color: #0000ff;"&gt;if</span> validcode.upper() != request.session[<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;keepValidCode</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;].upper(): </span><span style="color: #0000ff;"&gt;return</span> HttpResponse(json.dumps(<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;codeError</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;)) user </span>= authenticate(**<span style="color: #000000;"&gt;form.cleaned_data) </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; user=models.UserInfo.objects.filter(**form.cleaned_data)</span> <span style="color: #0000ff;"&gt;if</span> <span style="color: #0000ff;"&gt;not</span><span style="color: #000000;"&gt; user: </span><span style="color: #0000ff;"&gt;return</span> HttpResponse(json.dumps({<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;flag</span><span style="color: #800000;"&gt;'</span>: False,<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;msg</span><span style="color: #800000;"&gt;'</span>: <span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;用户名或者密码错误</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;})) </span><span style="color: #0000ff;"&gt;else</span><span style="color: #000000;"&gt;: login(request,user) </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;设置session </span> <span style="color: #0000ff;"&gt;return</span> HttpResponse(json.dumps({<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;flag</span><span style="color: #800000;"&gt;'</span>: True}))</pre>

 

  

  

img中src实现局部刷新验证码的功能

html部分:

"
看不清楚,换一张

js部分:

Highlighter_249123" class="SyntaxHighlighter js">
"

或:

//+=

这部分最重要的就是 $("#codePic").attr("src","http://127.0.0.1:8888/TP/codePic?flag="+Math.random()); 这部分的代码。如果不加flag="+Math.random()是实现不了局部刷新的功能的。因为。具体为什么会发生这种情况大家可以自己去研究。而codePic其实是一个action。这个action的功能是利用java画笔画出验证码并打包成图片返回给img中的src。

  

  

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐