Python生成验证码实例


Posted in Python onAugust 21, 2014

本文实例展示了Python生成验证码的方法,具有很好的实用价值。分享给大家供大家参考。具体实现方法如下:

前台页面代码如下:

<div>
 <img id="authcode_img" alt="验证码" src="/registration/makeimage/{{time}}"/>  
 <!-- time 任意随机数(时间戳),防止页面缓存 导致验证码不能更新-->
 <a href="javascript:refreshCode();" rel="external nofollow" style="color:blue;">看不清换一张</a>
</div>

<script>
 function refreshCode() {
   $('authcode_img').src = "/registration/makeimage/" + Math.random();
 }
</script>

后台程序如下:

import StringIO
import Image, ImageDraw, ImageFont, random  #相应的模块需要安装
from xxx.settings import authcode_font #请确保改字体存在

def make_image(request):
  mp = hashlib.md5()
  mp.update(str(datetime.datetime.now())+str(random.random()))  
  mp_src = mp.hexdigest()
  rand_str = mp_src[0:6]
  font = ImageFont.truetype(authcode_font, 25)
  width = 75
  height = 30
  im = Image.new('RGB',(width,height),'#%s'%mp_src[-7:-1])
  draw = ImageDraw.Draw(im)
  draw.line((random.randint(0,width),random.randint(0,height),random.randint(0,width),random.randint(0,height)))
  draw.line((random.randint(0,width),random.randint(0,height),random.randint(0,width),random.randint(0,height)))
  draw.line((random.randint(0,width),random.randint(0,height),random.randint(0,width),random.randint(0,height)))
  draw.line((random.randint(0,width),random.randint(0,height),random.randint(0,width),random.randint(0,height)))
  draw.line((random.randint(0,width),random.randint(0,height),random.randint(0,width),random.randint(0,height)))
  draw.text((5,2), rand_str, font=font)  
  del draw  
  buffer = StringIO.StringIO()
  im.save(buffer,'jpeg')
  httpResponse = HttpResponse(content=buffer.getvalue(),mimetype="image/jpeg")
  request.session['auth_code'] = rand_str
  return httpResponse

程序效果如下:

Python生成验证码实例

Python 相关文章推荐
零基础写python爬虫之神器正则表达式
Nov 06 Python
python微信跳一跳系列之自动计算跳一跳距离
Feb 26 Python
Python读取Excel表格,并同时画折线图和柱状图的方法
Oct 14 Python
理想高通滤波实现Python opencv示例
Jan 30 Python
Python爬虫——爬取豆瓣电影Top250代码实例
Apr 17 Python
Python可迭代对象操作示例
May 07 Python
python中使用ctypes调用so传参设置遇到的问题及解决方法
Jun 19 Python
Python文件时间操作步骤代码详解
Apr 13 Python
jupyter notebook实现显示行号
Apr 13 Python
Python devel安装失败问题解决方案
Jun 09 Python
python 抓取知乎指定回答下视频的方法
Jul 09 Python
教你利用python实现企业微信发送消息
May 23 Python
python中去空格函数的用法
Aug 21 #Python
python实现的重启关机程序实例
Aug 21 #Python
实例讲解Python中的私有属性
Aug 21 #Python
Python创建日历实例
Aug 21 #Python
Python实现批量把SVG格式转成png、pdf格式的代码分享
Aug 21 #Python
python生成日历实例解析
Aug 21 #Python
MySQLdb ImportError: libmysqlclient.so.18解决方法
Aug 21 #Python
You might like
PHP正则表达式之捕获组与非捕获组
2015/11/06 PHP
PHP数组的定义、初始化和数组元素的显示实现代码
2016/11/05 PHP
PHP实现文件下载【实例分享】
2017/04/28 PHP
详谈PHP中public,private,protected,abstract等关键字的用法
2017/12/31 PHP
超棒的javascript页面顶部卷动广告效果
2007/12/01 Javascript
Underscore.js 的模板功能介绍与应用
2012/12/24 Javascript
提交表单时执行func方法实现代码
2013/03/17 Javascript
js检测网络是否具体连接功能的代码
2014/05/23 Javascript
深入解析Javascript闭包的功能及实现方法
2016/07/10 Javascript
谈谈PHP中相对路径的问题与绝对路径的使用
2016/08/16 Javascript
使用JS批量选中功能实现更改数据库中的status状态值(批量展示)
2016/11/22 Javascript
Nodejs 获取时间加手机标识的32位标识实现代码
2017/03/07 NodeJs
基于JavaScript实现表格滚动分页
2017/11/22 Javascript
Bootstrap实现下拉菜单多级联动
2017/11/23 Javascript
解决JSON.stringify()自动将中文转译成unicode的问题
2018/01/05 Javascript
Angular数据绑定机制原理
2018/04/17 Javascript
微信小程序左滑删除实现代码实例
2019/09/16 Javascript
vue中的v-model原理,与组件自定义v-model详解
2020/08/04 Javascript
Python的Django中将文件上传至七牛云存储的代码分享
2016/06/03 Python
ubuntu中配置pyqt4环境教程
2017/12/27 Python
python email smtplib模块发送邮件代码实例
2018/04/26 Python
python3 unicode列表转换为中文的实例
2018/10/26 Python
使用Python和Prometheus跟踪天气的使用方法
2019/05/06 Python
详解pandas数据合并与重塑(pd.concat篇)
2019/07/09 Python
Python 项目转化为so文件实例
2019/12/23 Python
详解python如何引用包package
2020/06/07 Python
python 实现围棋游戏(纯tkinter gui)
2020/11/13 Python
办公室文秘自我评价
2013/09/21 职场文书
机械制造与自动化应届生求职信
2013/11/16 职场文书
春节联欢晚会主持词
2014/03/24 职场文书
党员批评与自我批评发言稿
2014/10/14 职场文书
夫妻分居协议书范文
2014/11/26 职场文书
班主任高考寄语
2015/02/26 职场文书
出国留学英文自荐信
2015/03/25 职场文书
Golang 1.18 多模块Multi-Module工作区模式的新特性
2022/04/11 Golang
如何利用python创作字符画
2022/06/25 Python