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 相关文章推荐
利用Hyperic调用Python实现进程守护
Jan 02 Python
10分钟教你用Python实现微信自动回复功能
Nov 28 Python
python3+opencv3识别图片中的物体并截取的方法
Dec 05 Python
浅谈python中真正关闭socket的方法
Dec 18 Python
解决Pycharm调用Turtle时 窗口一闪而过的问题
Feb 16 Python
Python获取Redis所有Key以及内容的方法
Feb 19 Python
python绘制双Y轴折线图以及单Y轴双变量柱状图的实例
Jul 08 Python
python实现输出一个序列的所有子序列示例
Nov 18 Python
Django 批量插入数据的实现方法
Jan 12 Python
pytorch实现对输入超过三通道的数据进行训练
Jan 15 Python
深入了解Python 变量作用域
Jul 24 Python
Python Pivot table透视表使用方法解析
Sep 11 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&amp;mysql(四)
2006/10/09 PHP
php输出xml格式字符串(用的这个)
2012/07/12 PHP
深入mysql_fetch_row()与mysql_fetch_array()的区别详解
2013/06/05 PHP
php变量与数组相互转换的方法(extract与compact)
2016/12/02 PHP
DWZ+ThinkPHP开发时遇到的问题分析
2016/12/12 PHP
PHP实现在windows下配置sendmail并通过mail()函数发送邮件的方法
2017/06/20 PHP
thinkPHP+mysql+ajax实现的仿百度一下即时搜索效果详解
2019/07/15 PHP
thinkphp5 框架结合plupload实现图片批量上传功能示例
2020/04/04 PHP
jquery对dom的操作常用方法整理
2013/06/25 Javascript
php结合imgareaselect实现图片裁剪
2015/07/05 Javascript
JavaScript forEach()遍历函数使用及介绍
2015/07/08 Javascript
Jquery组件easyUi实现选项卡切换示例
2016/08/23 Javascript
Angular2学习教程之ng中变更检测问题详解
2017/05/28 Javascript
Vue中如何实现轮播图的示例代码
2017/07/27 Javascript
使用vue-route 的 beforeEach 实现导航守卫(路由跳转前验证登录)功能
2018/03/22 Javascript
vue里的data要用return返回的原因浅析
2019/05/28 Javascript
小程序中this.setData的使用和注意事项
2019/08/28 Javascript
openlayers实现地图测距测面
2020/09/25 Javascript
python 动态获取当前运行的类名和函数名的方法
2014/04/15 Python
python两种遍历字典(dict)的方法比较
2014/05/29 Python
Python实现partial改变方法默认参数
2014/08/18 Python
python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题的解决方法
2014/08/22 Python
跟老齐学Python之Python文档
2014/10/10 Python
Python按行读取文件的简单实现方法
2016/06/22 Python
python3大文件解压和基本操作
2017/12/15 Python
10 行Python 代码实现 AI 目标检测技术【推荐】
2019/06/14 Python
详解Selenium+PhantomJS+python简单实现爬虫的功能
2019/07/14 Python
HTML5实现移动端复制功能
2018/04/19 HTML / CSS
西安众合通用.net笔试题
2013/03/18 面试题
港湾网络笔试题
2014/04/19 面试题
地理科学专业毕业生求职信
2013/10/15 职场文书
技术总监岗位职责
2013/12/05 职场文书
父亲的菜园教学反思
2014/02/13 职场文书
民主生活会整改措施(党员)
2014/09/18 职场文书
2014年护士工作总结范文
2014/11/11 职场文书
发票退票证明
2015/06/24 职场文书