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 切片和range()用法说明
Mar 24 Python
python使用mysqldb连接数据库操作方法示例详解
Dec 03 Python
Python使用Selenium+BeautifulSoup爬取淘宝搜索页
Feb 24 Python
TensorFlow 实战之实现卷积神经网络的实例讲解
Feb 26 Python
python异步实现定时任务和周期任务的方法
Jun 29 Python
python3 线性回归验证方法
Jul 09 Python
基于Python的微信机器人开发 微信登录和获取好友列表实现解析
Aug 21 Python
TensorFlow 显存使用机制详解
Feb 03 Python
python的help函数如何使用
Jun 11 Python
Python venv虚拟环境配置过程解析
Jul 08 Python
使用pandas实现筛选出指定列值所对应的行
Dec 13 Python
python程序实现BTC(比特币)挖矿的完整代码
Jan 20 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
攻克CakePHP系列三 表单数据增删改
2008/10/22 PHP
DW中链接mysql数据库时,建立字符集中文出现乱码的解决方法
2010/03/27 PHP
php页码形式分页函数支持静态化地址及ajax分页
2014/03/28 PHP
THINKPHP项目开发中的日志记录实例分析
2014/12/01 PHP
PHP给前端返回一个JSON对象的实例讲解
2018/05/31 PHP
解决laravel 表单提交-POST 异常的问题
2019/10/15 PHP
实例:用 JavaScript 来操作字符串(一些字符串函数)
2007/02/15 Javascript
jQuery学习3:操作元素属性和特性
2010/02/07 Javascript
js 实现css风格选择器(压缩后2KB)
2012/01/12 Javascript
批量实现面向对象的实例代码
2013/07/01 Javascript
使用JS取得焦点(focus)元素代码
2014/03/22 Javascript
jQuery制作可自定义大小的拼图游戏
2015/03/30 Javascript
JavaScript使用pop方法移除数组最后一个元素用法实例
2015/04/06 Javascript
常用DOM整理
2015/06/16 Javascript
jquery及js实现动态加载js文件的方法
2016/01/21 Javascript
学习使用jquery iScroll.js移动端滚动条插件
2020/03/24 Javascript
详解vue 中使用 AJAX获取数据的方法
2017/01/18 Javascript
微信小程序模板之分页滑动栏
2017/02/10 Javascript
js贪心算法 钱币找零问题代码实例
2019/09/11 Javascript
VueCli4项目配置反向代理proxy的方法步骤
2020/05/17 Javascript
python中sys.argv参数用法实例分析
2015/05/20 Python
python扫描proxy并获取可用代理ip的实例
2017/08/07 Python
Python cookbook(数据结构与算法)在字典中将键映射到多个值上的方法
2018/02/18 Python
python使用tcp实现局域网内文件传输
2020/03/20 Python
Pycharm运行加载文本出现错误的解决方法
2019/06/27 Python
python读取dicom图像示例(SimpleITK和dicom包实现)
2020/01/16 Python
自主招生自荐信格式
2013/12/03 职场文书
文秘专业应届生求职信
2014/05/26 职场文书
党支部活动策划方案
2014/08/18 职场文书
2015年幼儿园学期工作总结
2015/05/22 职场文书
幼儿教师师德培训心得体会
2016/01/09 职场文书
nginx网站服务如何配置防盗链(推荐)
2021/03/31 Servers
nginx location优先级的深入讲解
2021/03/31 Servers
sql通过日期判断年龄函数的示例代码
2021/07/16 SQL Server
git中cherry-pick命令的使用教程
2022/06/25 Servers
使用 CSS 构建强大且酷炫的粒子动画效果
2022/08/14 HTML / CSS