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 urllib模块urlopen()与urlretrieve()详解
Nov 01 Python
在Django中进行用户注册和邮箱验证的方法
May 09 Python
Python+selenium实现截图图片并保存截取的图片
Jan 05 Python
Python面向对象进阶学习
May 21 Python
PyQt5实现让QScrollArea支持鼠标拖动的操作方法
Jun 19 Python
python 利用pywifi模块实现连接网络破解wifi密码实时监控网络
Sep 16 Python
python getpass实现密文实例详解
Sep 24 Python
python字符串的拼接方法总结
Nov 18 Python
pytorch绘制并显示loss曲线和acc曲线,LeNet5识别图像准确率
Jan 02 Python
Python运行提示缺少模块问题解决方案
Apr 02 Python
python中not、and和or的优先级与详细用法介绍
Nov 03 Python
用python实现监控视频人数统计
May 21 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中的加密功能
2006/10/09 PHP
PHP Ajax中文乱码问题解决方法
2009/02/27 PHP
php缓冲 output_buffering的使用详解
2013/06/13 PHP
解析csv数据导入mysql的方法
2013/07/01 PHP
PHP curl CURLOPT_RETURNTRANSFER参数的作用使用实例
2015/02/07 PHP
PHP数组去重比较快的实现方式
2016/01/19 PHP
php使用PDO下exec()函数查询执行后受影响行数的方法
2017/03/28 PHP
Laravel Intervention/image图片处理扩展包的安装、使用与可能遇到的坑详解
2017/11/14 PHP
PHP htmlentities()函数用法讲解
2019/02/25 PHP
JS Date函数整理方便使用
2013/10/23 Javascript
JavaScript获取客户端计算机硬件及系统等信息的方法
2014/01/02 Javascript
js生成动态表格并为每个单元格添加单击事件的方法
2014/04/14 Javascript
Javascript基础知识(二)事件
2014/09/29 Javascript
jQuery实现将页面上HTML标签换成另外标签的方法
2015/06/09 Javascript
JavaScript实现图片瀑布流和底部刷新
2017/01/02 Javascript
JavaScript中从setTimeout与setInterval到AJAX异步
2017/02/13 Javascript
JavaScript之Map和Set_动力节点Java学院整理
2017/06/29 Javascript
JS中比Switch...Case更优雅的多条件判断写法
2019/09/05 Javascript
在Docker上开始部署Python应用的教程
2015/04/17 Python
详解python中的json的基本使用方法
2016/12/21 Python
python实现中文文本分句的例子
2019/07/15 Python
python 字符串常用函数详解
2019/09/11 Python
python求绝对值的三种方法小结
2019/12/04 Python
使用Python+Appuim 清理微信的方法
2021/01/26 Python
意大利会呼吸的鞋:Geox健乐士
2017/02/12 全球购物
Senreve官网:美国旧金山的奢侈手袋品牌
2019/03/21 全球购物
职业技术学校毕业生推荐信
2013/12/03 职场文书
我的网上商城创业计划书
2013/12/26 职场文书
迟到检讨书300字
2014/02/14 职场文书
经济贸易专业自荐信
2014/06/11 职场文书
中学学校门卫岗位职责
2014/08/15 职场文书
2014年文秘工作总结
2014/11/25 职场文书
建议书格式
2015/02/04 职场文书
幼师大班个人总结
2015/02/13 职场文书
撤诉状格式范本
2015/05/19 职场文书
2016党员发展对象培训心得体会
2016/01/08 职场文书