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 相关文章推荐
Django中实现点击图片链接强制直接下载的方法
May 14 Python
Python基于回溯法子集树模板解决马踏棋盘问题示例
Sep 11 Python
pandas pivot_table() 按日期分多列数据的方法
Nov 16 Python
python存储16bit和32bit图像的实例
Dec 05 Python
python消费kafka数据批量插入到es的方法
Dec 27 Python
在python下读取并展示raw格式的图片实例
Jan 24 Python
利用Python绘制Jazz网络图的例子
Nov 21 Python
pycharm激活码有效到2020年11月底
Sep 18 Python
Python3实现mysql连接和数据框的形成(实例代码)
Jan 17 Python
tensorflow2.0保存和恢复模型3种方法
Feb 03 Python
python用什么编辑器进行项目开发
Jun 17 Python
如何将json数据转换为python数据
Sep 04 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中的string类型使用说明
2010/07/27 PHP
什么是DOM(Document Object Model)文档对象模型
2012/03/05 Javascript
使用javascipt---实现二分查找法
2013/04/10 Javascript
js获取上传文件大小示例代码
2014/04/10 Javascript
jQuery数据缓存用法分析
2015/02/20 Javascript
基于JS代码实现实时显示系统时间
2016/06/16 Javascript
工作中比较实用的JavaScript验证和数据处理的干货(经典)
2016/08/03 Javascript
AngularJS在IE下取数据总是缓存问题的解决方法
2016/08/05 Javascript
javascript深拷贝(deepClone)详解
2016/08/24 Javascript
利用jQuery的动画函数animate实现豌豆发射效果
2016/08/28 Javascript
Vue.js仿Metronic高级表格(二)数据渲染
2017/04/19 Javascript
jQuery实现可兼容IE6的淡入淡出效果告警提示功能示例
2017/09/20 jQuery
Nodejs调用WebService的示例代码
2017/09/29 NodeJs
bootstrap时间插件daterangepicker使用详解
2017/10/19 Javascript
jquery+ajaxform+springboot控件实现数据更新功能
2018/01/22 jQuery
详解nodejs解压版安装和配置(带有搭建前端项目脚手架)
2018/12/06 NodeJs
微信小程序云开发之使用云函数
2019/05/17 Javascript
jqGrid表格底部汇总、合计行footerrow处理
2019/08/21 Javascript
Echarts地图添加引导线效果(labelLine)
2019/09/30 Javascript
微信小程序 scroll-view 水平滚动实现过程解析
2019/10/12 Javascript
python赋值操作方法分享
2013/03/23 Python
在Django的上下文中设置变量的方法
2015/07/20 Python
Python3实现的判断回文链表算法示例
2019/03/08 Python
python如何统计代码运行的时长
2019/07/24 Python
python中count函数知识点浅析
2020/12/17 Python
CSS3下的渐变文字效果实现示例
2018/03/02 HTML / CSS
Sam’s Club山姆会员商店:沃尔玛旗下高端会员制商店
2017/01/16 全球购物
集体婚礼证婚词
2014/01/13 职场文书
写自荐信的注意事项
2014/03/09 职场文书
博士生导师推荐信
2014/07/08 职场文书
学校组织向国旗敬礼活动方案(中小学适用)
2014/09/27 职场文书
党的群众路线教育实践活动调研报告
2014/11/03 职场文书
2014年小学体育工作总结
2014/12/11 职场文书
2015年发展党员工作总结报告
2015/03/31 职场文书
安全生产会议制度
2015/08/06 职场文书
MySQL定时备份数据库(全库备份)的实现
2021/09/25 MySQL