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可变参数*args和**kwargs用法实例小结
Apr 27 Python
django加载本地html的方法
May 27 Python
可能是最全面的 Python 字符串拼接总结【收藏】
Jul 09 Python
python字典的遍历3种方法详解
Aug 10 Python
python 利用pyttsx3文字转语音过程详解
Sep 25 Python
Python 安装 virturalenv 虚拟环境的教程详解
Feb 21 Python
python获取响应某个字段值的3种实现方法
Apr 30 Python
python进行参数传递的方法
May 12 Python
python json.dumps() json.dump()的区别详解
Jul 14 Python
如何以Winsows Service方式运行JupyterLab
Aug 30 Python
pandas:get_dummies()与pd.factorize()的用法及区别说明
May 21 Python
使用Python开发冰球小游戏
Apr 30 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开发不能违背的安全规则 过滤用户输入
2011/05/01 PHP
PHP中iconv函数转码时截断字符问题的解决方法
2015/01/21 PHP
PHP获取数组的键与值方法小结
2015/06/13 PHP
微信支付之JSAPI公众号支付详解
2019/05/15 PHP
ASP.NET jQuery 实例6 (实现CheckBoxList成员全选或全取消)
2012/01/13 Javascript
javascript操作ul中li的方法
2015/05/14 Javascript
深入解析JavaScript编程中的this关键字使用
2015/11/09 Javascript
javascript创建cookie、读取cookie
2016/03/31 Javascript
Js 获取当前函数参数对象的实现代码
2016/06/20 Javascript
javascript中Date对象应用之简易日历实现
2016/07/12 Javascript
vue实现ajax滚动下拉加载,同时具有loading效果(推荐)
2017/01/11 Javascript
ES6新特性二:Iterator(遍历器)和for-of循环详解
2017/04/20 Javascript
jQuery选择器_动力节点Java学院整理
2017/07/05 jQuery
微信小程序开发之animation循环动画实现的让云朵飘效果
2017/07/14 Javascript
es6中的解构赋值、扩展运算符和rest参数使用详解
2017/09/28 Javascript
js定时器+简单的动画效果实例
2017/11/10 Javascript
vue路由拦截及页面跳转的设置方法
2018/05/24 Javascript
浅谈angular2子组件的事件传递(任意组件事件传递)
2018/09/30 Javascript
新手快速入门微信小程序组件库 iView Weapp
2019/06/24 Javascript
解决LayUI加上form.render()下拉框和单选以及复选框不出来的问题
2019/09/27 Javascript
Vue实现简单的跑马灯
2020/05/25 Javascript
Python实现中文数字转换为阿拉伯数字的方法示例
2017/05/26 Python
python3 发送任意文件邮件的实例
2018/01/23 Python
python实现梯度下降算法
2020/03/24 Python
python树莓派红外反射传感器
2019/01/21 Python
pyqt5 实现多窗口跳转的方法
2019/06/19 Python
深入浅析python3中的unicode和bytes问题
2019/07/03 Python
详解使用python绘制混淆矩阵(confusion_matrix)
2019/07/14 Python
用Pytorch训练CNN(数据集MNIST,使用GPU的方法)
2019/08/19 Python
Python pip 安装与使用(安装、更新、删除)
2019/10/06 Python
python实现将中文日期转换为数字日期
2020/07/14 Python
Python无损压缩图片的示例代码
2020/08/06 Python
CSS3实现瀑布流布局与无限加载图片相册的实例代码
2016/12/22 HTML / CSS
美国存储和组织商店:The Container Store
2017/08/16 全球购物
紧急迫降观后感
2015/06/15 职场文书
利用前端HTML+CSS+JS开发简单的TODOLIST功能(记事本)
2021/04/13 Javascript