• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python生成验证码实例

Python生成验证码实例

作者:shichen2014 字体:[增加 减小] 来源:互联网

shichen2014 通过本文主要向大家介绍了python项目实例,python脚本编写实例,python开发网站实例,python开发实例,python多线程实例等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例展示了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>

</div>

后台程序如下:

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

</div>

程序效果如下:

</div>

您可能想查找下面的文章:

  • Python只用40行代码编写的计算器实例
  • python获取当前运行函数名称的方法实例代码
  • Python正则简单实例分析
  • 利用Python获取操作系统信息实例
  • Python过滤列表用法实例分析
  • Python过滤列表用法实例分析
  • 实例讲解Python中函数的调用与定义
  • 实例解析Python设计模式编程之桥接模式的运用
  • 实例讲解Python中函数的调用与定义
  • Python聊天室实例程序分享

相关文章

  • Python中的列表生成式与生成器学习教程
  • Python实现类继承实例
  • python中私有函数调用方法解密
  • python读取csv文件示例(python操作csv)
  • 一个基于flask的web应用诞生 记录用户账户登录状态(6)
  • python创建临时文件夹的方法
  • Python学习资料
  • python装饰器与递归算法详解
  • Python3实现并发检验代理池地址的方法
  • Python编程中对文件和存储器的读写示例

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • python求斐波那契数列示例分享
    • 详解Python编程中基本的数学计算使用
    • 浅析Python中yield关键词的作用与用法
    • python中dir函数用法分析
    • python多进程操作实例
    • 用Python的Tornado框架结合memcached页面改善博客性能
    • 开源Web应用框架Django图文教程
    • python笔记(1) 关于我们应不应该继续学习python
    • python列表操作使用示例分享
    • 解读Django框架中的低层次缓存API

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有