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在windows下实现ping操作并接收返回信息的方法
Mar 20 Python
Python编写简单的HTML页面合并脚本
Jul 11 Python
Python 写入训练日志文件并控制台输出解析
Aug 13 Python
python基于三阶贝塞尔曲线的数据平滑算法
Dec 27 Python
Tensorflow 使用pb文件保存(恢复)模型计算图和参数实例详解
Feb 11 Python
利用python生成照片墙的示例代码
Apr 09 Python
解决jupyter notebook 前面书写后面内容消失的问题
Apr 13 Python
使用Django搭建网站实现商品分页功能
May 22 Python
python中adb有什么功能
Jun 07 Python
python/golang 删除链表中的元素
Sep 14 Python
两行代码解决Jupyter Notebook中文不能显示的问题
Apr 24 Python
python基于机器学习预测股票交易信号
May 25 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
怎样在UNIX系统下安装php3
2006/10/09 PHP
PHP查询MySQL大量数据的时候内存占用分析
2011/07/22 PHP
destoon实现资讯信息前面调用它所属分类的方法
2014/07/15 PHP
destoon切换城市后实现logo旁边显示地区名称的方法
2014/08/21 PHP
php将HTML表格每行每列转为数组实现采集表格数据的方法
2015/04/03 PHP
如何用ajax来创建一个XMLHttpRequest对象
2012/12/10 Javascript
子页向父页传值示例
2013/11/27 Javascript
jQuery实现表格颜色交替显示的方法
2015/03/09 Javascript
jquery实现定时自动轮播特效
2015/12/10 Javascript
微信小程序 商城开发(ecshop )简单实例
2017/04/07 Javascript
vue使用ElementUI时导航栏默认展开功能的实现
2018/07/04 Javascript
微信小程序实现点击空白隐藏的方法示例
2019/08/13 Javascript
layui 监听select选择 获取当前select的ID名称方法
2019/09/24 Javascript
微信小程序 bindtap 传参的实例代码
2020/02/21 Javascript
javascript设计模式 ? 外观模式原理与用法实例分析
2020/04/15 Javascript
Python抓取淘宝下拉框关键词的方法
2015/07/08 Python
Python中shape计算矩阵的方法示例
2017/04/21 Python
Python3安装Pymongo详细步骤
2017/05/26 Python
Python编程实现从字典中提取子集的方法分析
2018/02/09 Python
int在python中的含义以及用法
2019/06/27 Python
python运用pygame库实现双人弹球小游戏
2019/11/25 Python
使用pyshp包进行shapefile文件修改的例子
2019/12/06 Python
django xadmin 管理器常用显示设置方式
2020/03/11 Python
JRE、JDK、JVM之间的关系怎样
2012/05/16 面试题
打架检讨书100字
2014/01/08 职场文书
大二自我鉴定
2014/01/31 职场文书
市场部管理制度
2014/02/02 职场文书
《理想》教学反思
2014/02/17 职场文书
《乞巧》教学反思
2014/02/27 职场文书
工程项目建议书范文
2014/03/12 职场文书
幼儿园社区活动总结
2014/07/07 职场文书
助人为乐好少年事迹材料
2014/08/18 职场文书
2014年“向国旗敬礼”网上签名寄语活动方案
2014/09/27 职场文书
运动会搞笑广播稿
2014/10/14 职场文书
学术会议邀请函
2015/01/30 职场文书
Python利用FlashText算法实现替换字符串
2022/03/31 Python