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创建只读属性对象的方法(ReadOnlyObject)
Feb 10 Python
常用python编程模板汇总
Feb 12 Python
selenium python浏览器多窗口处理代码示例
Jan 15 Python
Numpy 将二维图像矩阵转换为一维向量的方法
Jun 05 Python
Python面向对象类的继承实例详解
Jun 27 Python
Python读取excel指定列生成指定sql脚本的方法
Nov 28 Python
django orm 通过related_name反向查询的方法
Dec 15 Python
python面向对象 反射原理解析
Aug 12 Python
基于python实现对文件进行切分行
Apr 26 Python
Keras自定义IOU方式
Jun 10 Python
matplotlib subplot绘制多个子图的方法示例
Jul 28 Python
Python类方法总结讲解
Jul 26 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无刷新上传文件实现代码
2011/09/19 PHP
PHP 读取大文件的X行到Y行内容的实现代码
2013/06/24 PHP
php实现高效获取图片尺寸的方法
2014/12/12 PHP
php HTML无刷新提交表单
2016/04/05 PHP
Laravel程序架构设计思路之使用动作类
2018/06/07 PHP
php web环境和命令行环境下查找php.ini的位置
2019/07/17 PHP
在textarea文本域中显示HTML代码的方法
2007/03/06 Javascript
33种Javascript 表格排序控件收集
2009/12/03 Javascript
JavaScript打开word文档的实现代码(c#)
2012/04/16 Javascript
JavaScript实现Java中StringBuffer的方法
2015/02/09 Javascript
jQuery获取某天的农历日期并判断是否除夕或新年的方法
2016/03/01 Javascript
Bootstrap table 定制提示语的加载过程
2017/02/20 Javascript
详解React之父子组件传递和其它一些要点
2018/06/25 Javascript
使用mpvue搭建一个初始小程序及项目配置方法
2018/12/03 Javascript
微信小程序学习笔记之函数定义、页面渲染图文详解
2019/03/28 Javascript
微信小程序实现卡片层叠滑动效果
2019/06/21 Javascript
vue使用微信JS-SDK实现分享功能
2019/08/23 Javascript
[03:44]2014DOTA2国际邀请赛 71专访:DK战队赛前讨论视频遭泄露
2014/07/13 DOTA
玩转python爬虫之爬取糗事百科段子
2016/02/17 Python
Saltstack快速入门简单汇总
2016/03/01 Python
python3写爬取B站视频弹幕功能
2017/12/22 Python
Windows上使用Python增加或删除权限的方法
2018/04/24 Python
python使用Plotly绘图工具绘制柱状图
2019/04/01 Python
python实现单机五子棋
2020/08/28 Python
HTML5中form如何关闭自动完成功能的方法
2018/07/02 HTML / CSS
Fossil德国官网:化石手表、手袋、珠宝及配件
2019/12/07 全球购物
限量版运动鞋和街头服饰:TheDrop
2020/09/06 全球购物
法人授权委托书
2014/04/03 职场文书
《生命的药方》教学反思
2014/04/08 职场文书
师德师风个人反思
2014/04/28 职场文书
2014七年级班主任工作总结
2014/12/05 职场文书
2015年全国科普日活动总结
2015/03/23 职场文书
2015小学教师年度工作总结
2015/05/12 职场文书
消防验收申请报告
2015/05/15 职场文书
原告离婚代理词
2015/05/23 职场文书
学校就业保障协议书
2019/06/24 职场文书