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中使用xlrd、xlwt操作excel表格详解
Jan 29 Python
在Python下进行UDP网络编程的教程
Apr 29 Python
python如何查看系统网络流量的信息
Sep 12 Python
Django原生sql也能使用Paginator分页的示例代码
Nov 15 Python
OpenCV-Python实现轮廓检测实例分析
Jan 05 Python
使用pandas中的DataFrame数据绘制柱状图的方法
Apr 10 Python
总结python中pass的作用
Feb 27 Python
python定时复制远程文件夹中所有文件
Apr 30 Python
PyCharm2018 安装及破解方法实现步骤
Sep 09 Python
Pytorch通过保存为ONNX模型转TensorRT5的实现
May 25 Python
基于Python3读写INI配置文件过程解析
Jul 23 Python
python自动统计zabbix系统监控覆盖率的示例代码
Apr 03 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微信开发之二维码生成类
2015/06/26 PHP
Json_decode 解析json字符串为NULL的解决方法(必看)
2017/02/17 PHP
thinkPHP5.0框架验证码调用及点击图片刷新简单实现方法
2018/09/07 PHP
PHP中md5()函数的用法讲解
2019/03/30 PHP
gearman中worker常驻后台,导致MySQL server has gone away的解决方法
2020/02/27 PHP
jQuery 加上最后自己的验证
2009/11/04 Javascript
动态加载js和css(外部文件)
2013/04/17 Javascript
20行代码实现的一个CSS覆盖率测试脚本
2013/07/07 Javascript
jQuery标签替换函数replaceWith()的使用例子
2014/08/28 Javascript
jscript读写二进制文件的方法
2015/04/22 Javascript
JS+DIV实现鼠标划过切换层效果的方法
2015/05/25 Javascript
js实现超简单的展开、折叠目录代码
2015/08/28 Javascript
IE8下jQuery改变png图片透明度时出现的黑边
2015/08/30 Javascript
jQuery prototype冲突的2种解决方法(附demo示例下载)
2016/01/21 Javascript
JS延时器提示框的应用实例代码解析
2016/04/27 Javascript
微信小程序 video详解及简单实例
2017/01/16 Javascript
nodejs入门教程五:连接数据库的方法分析
2017/04/24 NodeJs
xmlplus组件设计系列之分隔框(DividedBox)(8)
2017/05/02 Javascript
vue和H5 draggable实现拖拽并替换效果
2020/07/29 Javascript
在Python的Django框架中simple-todo工具的简单使用
2015/05/30 Python
详解Python的Flask框架中生成SECRET_KEY密钥的方法
2016/06/07 Python
PyQt5 pyqt多线程操作入门
2018/05/05 Python
Python爬虫入门有哪些基础知识点
2020/06/02 Python
Python 字符串池化的前提
2020/07/03 Python
python 利用panda 实现列联表(交叉表)
2021/02/06 Python
纯DOM+CSS3实现简单的小风车动画
2016/09/27 HTML / CSS
全球领先的全景影像品牌:Insta360
2019/08/21 全球购物
Tuckernuck官网:经典的美国品质服装、鞋子和配饰
2021/01/11 全球购物
舞蹈毕业生的自我评价
2014/03/05 职场文书
小学开学典礼主持词
2014/03/19 职场文书
初中国旗下的演讲稿
2014/08/28 职场文书
公司感恩节活动策划书
2014/10/11 职场文书
巾帼标兵事迹材料
2014/12/26 职场文书
唐山大地震观后感
2015/06/05 职场文书
2016年中学法制宣传日活动总结
2016/04/01 职场文书
Django debug为True时,css加载失败的解决方案
2021/04/24 Python