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下进行UDP网络编程的教程
Apr 29 Python
python 2.6.6升级到python 2.7.x版本的方法
Oct 09 Python
python绘制铅球的运行轨迹代码分享
Nov 14 Python
Python+树莓派+YOLO打造一款人工智能照相机
Jan 02 Python
Python使用Django实现博客系统完整版
Sep 29 Python
Python multiprocess pool模块报错pickling error问题解决方法分析
Mar 20 Python
python3利用Socket实现通信的方法示例
May 06 Python
python flask 如何修改默认端口号的方法步骤
Jul 12 Python
TensorFlow绘制loss/accuracy曲线的实例
Jan 21 Python
浅析Django 接收所有文件,前端展示文件(包括视频,文件,图片)ajax请求
Mar 09 Python
Python多线程threading创建及使用方法解析
Jun 17 Python
python实现简单反弹球游戏
Apr 12 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函数间的参数传递(值传递/引用传递)
2013/09/23 PHP
PHP生成数组再传给js的方法
2014/08/07 PHP
php对文件夹进行相关操作(遍历、计算大小)
2015/11/04 PHP
学习php设计模式 php实现抽象工厂模式
2015/12/07 PHP
PHP屏蔽关键字实现方法
2016/11/17 PHP
Yaf框架封装的MySQL数据库操作示例
2019/03/06 PHP
简洁Ajax函数处理(示例代码)
2013/11/15 Javascript
让jQuery与其他JavaScript库并存避免冲突的方法
2013/12/23 Javascript
原生js和jquery中有关透明度设置的相关问题
2014/01/08 Javascript
jquery简单实现网页层的展开与收缩效果
2015/08/07 Javascript
JS提交form表单实例分析
2015/12/10 Javascript
JavaScript中三种异步上传文件方式
2016/03/06 Javascript
Js 获取、判断浏览器版本信息的简单方法
2016/08/08 Javascript
Bootstrap实现带暂停功能的轮播组件(推荐)
2016/11/25 Javascript
提高Web性能的前端优化技巧总结
2017/02/27 Javascript
详解使用grunt完成requirejs的合并压缩和js文件的版本控制
2017/03/02 Javascript
JavaScript实现简单的四则运算计算器完整实例
2017/04/28 Javascript
Web技术实现移动监测的介绍
2017/09/18 Javascript
使用ef6创建oracle数据库的实体模型遇到的问题及解决方案
2017/11/09 Javascript
使用element-ui table expand展开行实现手风琴效果
2019/03/15 Javascript
NodeJs之word文件生成与解析的实现代码
2019/04/01 NodeJs
JavaScript如何把两个数组对象合并过程解析
2019/10/10 Javascript
微信小程序可滑动月日历组件使用详解
2019/10/21 Javascript
python编程使用协程并发的优缺点
2018/09/20 Python
python使用itchat模块给心爱的人每天发天气预报
2019/11/25 Python
Django集成celery发送异步邮件实例
2019/12/17 Python
TFRecord文件查看包含的所有Features代码
2020/02/17 Python
Matplotlib animation模块实现动态图
2021/02/25 Python
从零实现一个自定义html5播放器的示例代码
2017/08/01 HTML / CSS
英国最受欢迎的在线隐形眼镜商店:VisionDirect.co.uk
2018/12/06 全球购物
台湾租车首选品牌:IWS艾维士租车
2019/05/03 全球购物
慕尼黑山地运动、户外服装和体育用品专家:Sporthaus Schuster
2019/08/27 全球购物
UNIX文件系统分类
2014/11/11 面试题
行政部岗位职责范本
2014/03/13 职场文书
初中生庆国庆演讲稿范文2014
2014/09/25 职场文书
2014年心理健康教育工作总结
2014/12/06 职场文书