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根据经纬度计算距离示例
Feb 16 Python
python中xrange用法分析
Apr 15 Python
PyQt5主窗口动态加载Widget实例代码
Feb 07 Python
tensorflow: 查看 tensor详细数值方法
Jun 13 Python
python3使用matplotlib绘制条形图
Mar 25 Python
Python代码实现删除一个list里面重复元素的方法
Apr 02 Python
python使用装饰器作日志处理的方法
Jul 11 Python
Python Request爬取seo.chinaz.com百度权重网站的查询结果过程解析
Aug 13 Python
Python列表的切片实例讲解
Aug 20 Python
Python之——生成动态路由轨迹图的实例
Nov 22 Python
Python简单实现区域生长方式
Jan 16 Python
浅谈Python中re.match()和re.search()的使用及区别
Apr 14 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
ubuntu下编译安装xcache for php5.3 的具体操作步骤
2013/06/18 PHP
javascript Zifa FormValid 0.1表单验证 代码打包下载
2007/06/08 Javascript
Javascript 代码也可以变得优美的实现方法
2009/06/22 Javascript
jquery 学习之一 对象访问
2010/11/23 Javascript
初学js插入节点appendChild insertBefore使用方法
2011/07/04 Javascript
js URL参数的拼接方法比较
2012/02/15 Javascript
js获取元素外链样式的方法
2015/01/27 Javascript
javascript中一些util方法汇总
2015/06/10 Javascript
详解JavaScript的Polymer框架中的通知交互
2015/07/29 Javascript
jQuery图片轮播功能实例代码
2017/01/29 Javascript
从零开始做一个pagination分页组件
2017/03/15 Javascript
浅谈Angular 中何时取消订阅
2017/11/22 Javascript
JavaScript生成指定范围随机数和随机序列的方法
2018/05/05 Javascript
webpack4+react多页面架构的实现
2018/10/25 Javascript
JS滚轮控制图片缩放大小和拖动的实例代码
2018/11/20 Javascript
layui禁用侧边导航栏点击事件的解决方法
2019/09/25 Javascript
Javascript模块化机制实现原理详解
2020/04/02 Javascript
[31:33]2014 DOTA2国际邀请赛中国区预选赛 TongFu VS DT 第一场
2014/05/23 DOTA
Python学习笔记(一)(基础入门之环境搭建)
2014/06/05 Python
Python入门篇之面向对象
2014/10/20 Python
浅谈python新式类和旧式类区别
2019/04/26 Python
python自定义时钟类、定时任务类
2021/02/22 Python
ipad上运行python的方法步骤
2019/10/12 Python
在python3.64中安装pyinstaller库的方法步骤
2020/06/02 Python
英国名牌服装购物网站:OD’s Designer
2019/09/02 全球购物
英国最大的滑板品牌选择:Route One
2019/09/22 全球购物
Farfetch澳大利亚官网:Farfetch Australia
2020/04/26 全球购物
英国最大的在线照明商店:Litecraft
2020/08/31 全球购物
写一个函数,求一个字符串的长度。在main函数中输入字符串,并输出其长度
2015/11/18 面试题
explicit和implicit的含义
2012/11/15 面试题
运动会致辞稿50字
2014/02/04 职场文书
安全生产中长期规划实施方案
2014/02/21 职场文书
导师就业推荐信范文
2014/05/22 职场文书
CAD实训总结范文
2015/08/03 职场文书
干货:如何写好工作计划!
2019/05/17 职场文书
实例讲解Python中sys.argv[]的用法
2021/06/03 Python