图片验证码

生成图片验证码需要以下:

  • session
  • check_code.py(依赖:Pillow,字体文件) 模块安装 pip install Pillow
  • src属性后面加?

在utils下拷贝check_code.py(用于生成图片验证码)文件

#!/usr/bin/env python
# -*- coding:utf-8 -*- import random
from PIL import Image, ImageDraw, ImageFont, ImageFilter _letter_cases = "abcdefghjkmnpqrstuvwxy" # 小写字母,去除可能干扰的i,l,o,z
_upper_cases = _letter_cases.upper() # 大写字母
_numbers = ''.join(map(str, range(3, 10))) # 数字
init_chars = ''.join((_letter_cases, _upper_cases, _numbers)) def create_validate_code(size=(120, 30),
chars=init_chars,
img_type="GIF",
mode="RGB",
bg_color=(255, 255, 255),
fg_color=(0, 0, 255),
font_size=18,
font_type="Monaco.ttf",
length=4,
draw_lines=True,
n_line=(1, 2),
draw_points=True,
point_chance=2):
"""
@todo: 生成验证码图片
@param size: 图片的大小,格式(宽,高),默认为(120, 30)
@param chars: 允许的字符集合,格式字符串
@param img_type: 图片保存的格式,默认为GIF,可选的为GIF,JPEG,TIFF,PNG
@param mode: 图片模式,默认为RGB
@param bg_color: 背景颜色,默认为白色
@param fg_color: 前景色,验证码字符颜色,默认为蓝色#0000FF
@param font_size: 验证码字体大小
@param font_type: 验证码字体,默认为 ae_AlArabiya.ttf
@param length: 验证码字符个数
@param draw_lines: 是否划干扰线
@param n_lines: 干扰线的条数范围,格式元组,默认为(1, 2),只有draw_lines为True时有效
@param draw_points: 是否画干扰点
@param point_chance: 干扰点出现的概率,大小范围[0, 100]
@return: [0]: PIL Image实例
@return: [1]: 验证码图片中的字符串
""" width, height = size # 宽高
# 创建图形
img = Image.new(mode, size, bg_color)
draw = ImageDraw.Draw(img) # 创建画笔 def get_chars():
"""生成给定长度的字符串,返回列表格式"""
return random.sample(chars, length) def create_lines():
"""绘制干扰线"""
line_num = random.randint(*n_line) # 干扰线条数 for i in range(line_num):
# 起始点
begin = (random.randint(0, size[0]), random.randint(0, size[1]))
# 结束点
end = (random.randint(0, size[0]), random.randint(0, size[1]))
draw.line([begin, end], fill=(0, 0, 0)) def create_points():
"""绘制干扰点"""
chance = min(100, max(0, int(point_chance))) # 大小限制在[0, 100] for w in range(width):
for h in range(height):
tmp = random.randint(0, 100)
if tmp > 100 - chance:
draw.point((w, h), fill=(0, 0, 0)) def create_strs():
"""绘制验证码字符"""
c_chars = get_chars()
strs = ' %s ' % ' '.join(c_chars) # 每个字符前后以空格隔开 font = ImageFont.truetype(font_type, font_size)
font_width, font_height = font.getsize(strs) draw.text(((width - font_width) / 3, (height - font_height) / 3),
strs, font=font, fill=fg_color) return ''.join(c_chars) if draw_lines:
create_lines()
if draw_points:
create_points()
strs = create_strs() # 图形扭曲参数
params = [1 - float(random.randint(1, 2)) / 100,
0,
0,
0,
1 - float(random.randint(1, 10)) / 100,
float(random.randint(1, 2)) / 500,
0.001,
float(random.randint(1, 2)) / 500
]
img = img.transform(size, Image.PERSPECTIVE, params) # 创建扭曲 img = img.filter(ImageFilter.EDGE_ENHANCE_MORE) # 滤镜,边界加强(阈值更大) return img, strs

check_code.py

templates下login.html文件

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="/static/plugins/bootstrap/css/bootstrap.css"/>
<link rel="stylesheet" href="/static/plugins/font-awesome/css/font-awesome.css"/>
<link rel="stylesheet" href="/static/css/edmure.css"/>
<link rel="stylesheet" href="/static/css/commons.css"/>
<link rel="stylesheet" href="/static/css/account.css"/>
<style> </style>
</head>
<body>
<div class="login">
<div style="font-size: 25px; font-weight: bold;text-align: center;">
用户登陆
</div>
<form role="form" action="/login.html" method="POST">
{% csrf_token %}
<div class="form-group">
<label for="username">用户名</label>
<input type="text" class="form-control" placeholder="请输入用户名">
</div>
<div class="form-group">
<label for="password">密码</label>
<input type="password" class="form-control" placeholder="请输入密码">
</div>
<div class="form-group">
<label for="password">验证码</label> <div class="row">
<div class="col-xs-7">
<input type="text" class="form-control" placeholder="请输入验证码" name="check_code">
</div>
<div class="col-xs-5">
<img src="/check_code.html" onclick="changeCheckCode(this);"> <!--点击更换验证码-->
</div>
</div> </div>
<div class="checkbox">
<label>
<input type="checkbox"> 一个月内自动登陆
</label>
<div class="right">
<a href="#">忘记密码?</a>
</div>
</div>
<button type="submit" class="btn btn-default">登 陆</button>
</form>
</div>
<script>
function changeCheckCode(ths){
ths.src = ths.src + '?'; //刷新验证码
}
</script>
</body>
</html>

login.html

文件关键的两个点:

<div class="col-xs-5">
<img src="/check_code.html" onclick="changeCheckCode(this);"> <!--点击更换验证码-->
</div>
<script>
function changeCheckCode(ths){
ths.src = ths.src + '?'; //刷新验证码
}
</script>

处理文件account.py

from io import BytesIO
from django.shortcuts import HttpResponse
from django.shortcuts import render
from utils.check_code import create_validate_code def check_code(request):
"""
验证码
:param request:
:return:
"""
# 1. 创建一张图片 pip3 install Pillow
# 2. 在图片中写入随机字符串
# obj = object()
# 3. 将图片写入到制定文件
# 4. 打开制定目录文件,读取内容
# 5. HttpResponse(data) stream = BytesIO() #在内存中生成一个文件对象
img, code = create_validate_code() #生成图片img和字符串code
img.save(stream,'PNG') #把验证图片存放到内存中以PNG名存放
request.session['CheckCode'] = code #把生成的字符串code存放到session中
print(code)
return HttpResponse(stream.getvalue()) #stream.getvalue()返回图片的内容 def login(request):
"""
登陆
:param request:
:return:
"""
if request.method == 'GET':
return render(request, 'login.html')
elif request.method == 'POST':
# 此次省略用户名密码验证
code = request.POST.get('check_code')
if code.upper() == request.session['CheckCode'].upper():
print('验证码正确')
return HttpResponse('OK')
else:
print('验证码错误')
return render(request, 'login.html')

上述操作可完成图片验证的认证 

KindEditor文件编辑器

1、进入官网

2、下载

  • 官网下载:http://kindeditor.net/down.php
  • 本地下载:http://files.cnblogs.com/files/wupeiqi/kindeditor_a5.zip

3、文件夹说明

├── asp                          asp示例         可删
├── asp.net asp.net示例 可删
├── attached 空文件夹,放置关联文件attached 可删
├── examples HTML示例 可删
├── jsp java示例 可删
├── kindeditor-all-min.js 全部JS(压缩)
├── kindeditor-all.js 全部JS(未压缩)
├── kindeditor-min.js 仅KindEditor JS(压缩)
├── kindeditor.js 仅KindEditor JS(未压缩)
├── lang 支持语言
├── license.txt License
├── php PHP示例 可删
├── plugins KindEditor内部使用的插件
└── themes KindEditor主题

4、基本使用  

把下载的kind-editor文件拷贝到static目录下

<textarea name="content" id="content"></textarea>

<script src="/static/jquery-1.12.4.js"></script>
<script src="/static/plugins/kind-editor/kindeditor-all.js"></script>
<script>
$(function () {
initKindEditor();
}); function initKindEditor() {
// 第一个参数id content 第二个参数字典 进行配置
var kind = KindEditor.create('#content', {
width: '100%', // 文本框宽度(可以百分比或像素)
height: '300px', // 文本框高度(只能像素)
minWidth: 200, // 最小宽度(数字)
minHeight: 400 // 最小高度(数字)
{# items: ['superscript', 'clearhtml', 'quickformat', 'selectall'] ,// 配置工具栏#}
{# noDisableItems: ["source", "fullscreen"], // 得和designMode:false结合使用 表示只显示那些工具#}
{# designMode: false,#} });
}
</script>

重点:kind-editor的详细参数--》》http://kindeditor.net/docs/option.html

5、上传文件+文件空间管理

HTML文件:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form>
{% csrf_token %}
<div style="width: 500px;margin: 0 auto">
<textarea id="content"></textarea>
</div>
<input type="submit" value="提交"/>
</form> <script src="/static/jquery-1.12.4.js"></script>
<script src="/static/kindeditor-4.1.10/kindeditor-all.js"></script>
<script>
$(function () {
// 第一个参数id content 第二个参数字典 进行配置
KindEditor.create('#content', {
uploadJson: '/upload_img/', // 上传地址 图片?dir=image 不同类型dir不一样
fileManagerJson: '/file_manager/', // 文件空间管理
allowImageRemote: true, // 是否允许上传远程图片
allowImageUpload: true, // 是否允许上传图片
allowFileManager: true,
extraFileUploadParams: { // 这个比较重要,提交csrf_token
csrfmiddlewaretoken: "{{ csrf_token }}"
},
filePostName: 'fafafa' // 发送文件名
});
})
</script>
</body>
</html>

处理文件:

def kind(request):

    return render(request,'kind.html')

def upload_img(request):
# 文件上传
print(request.FILES)
# < MultiValueDict: {'fafafa': [ < InMemoryUploadedFile: 20170213143925.png(image / png) >]} >
dic = {
'error': 0, # 0表示正确 1为错误
'url': '/static/imgs/20170213143925.png', #预览图片地址
'message': '错误了...'
}
import json
return HttpResponse(json.dumps(dic)) import os
import time
import json
def file_manager(request):
"""
文件管理
:param request:
:return:
"""
dic = {}
root_path = 'C:/Users/L/PycharmProjects/Django项目'
static_root_path = '/static/'
request_path = request.GET.get('path')
if request_path:
abs_current_dir_path = os.path.join(root_path, request_path)
move_up_dir_path = os.path.dirname(request_path.rstrip('/'))
dic['moveup_dir_path'] = move_up_dir_path + '/' if move_up_dir_path else move_up_dir_path else:
abs_current_dir_path = root_path
dic['moveup_dir_path'] = '' dic['current_dir_path'] = request_path
dic['current_url'] = os.path.join(static_root_path, request_path) file_list = []
for item in os.listdir(abs_current_dir_path):
abs_item_path = os.path.join(abs_current_dir_path, item)
a, exts = os.path.splitext(item)
is_dir = os.path.isdir(abs_item_path)
if is_dir:
temp = {
'is_dir': True,
'has_file': True,
'filesize': 0,
'dir_path': '',
'is_photo': False,
'filetype': '',
'filename': item,
'datetime': time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(os.path.getctime(abs_item_path)))
}
else:
temp = {
'is_dir': False,
'has_file': False,
'filesize': os.stat(abs_item_path).st_size,
'dir_path': '',
'is_photo': True if exts.lower() in ['.jpg', '.png', '.jpeg'] else False,
'filetype': exts.lower().strip('.'),
'filename': item,
'datetime': time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(os.path.getctime(abs_item_path)))
} file_list.append(temp)
dic['file_list'] = file_list
return HttpResponse(json.dumps(dic))

  

《第二十四章》

Python开发【Django】:图片验证码、KindEditor的更多相关文章

  1. Python 实现简单图片验证码登录

    朋友说公司要在测试环境做接口测试,登录时需要传入正确的图片的验证码,本着懒省事的原则,推荐他把测试环境的图片验证码写死,我们公司也是这么做的^_^.劝说无果/(ㄒoㄒ)/~~,只能通过 OCR 技术来 ...

  2. 【python】带图片验证码的登录自动化实战

    近期在跟进新项目的时候,整体的业务线非常之长,会一直重复登录退出不同账号的这个流程,所以想从登录开始实现部分的自动化.因为是B/S的架构,所以采用的是selenium的框架来实现.大致实现步骤如下: ...

  3. 使用.Net Core 2.1开发Captcha图片验证码服务

    更新后续篇:Captcha服务(后续1) 使用.Net Core 2.1开发Captcha验证码服务 开发工具:Visual Studio 2017 15.7.3 开发平台:64位 Windows 1 ...

  4. JMeter开发插件——图片验证码识别

    我们在性能测试中总会时不时地遭遇到来自于应用系统的各种阻碍,图片验证码就是一类最常见的束缚,登录或交易时需要按照图片中的内容输入正确的验证信息后,数据才可以提交成功,这使得许多性能测试工具只能望而却步 ...

  5. 【转】Python OCR识别图片验证码

    转载自:博客 对于某些网站登录的时候,往往需要输入验证码才能实现登录.如果要爬虫这类网站,往往总会比这个验证码导致无法爬取数据.以下介绍一种比较折中的方法,也是比较可行的方法: 实现思想: 1.通过截 ...

  6. python 生成随机图片验证码

    1.安装pillow模块 pip install pillow (1)创建图片 from PIL import Image #定义使用Image类实例化一个长为400px,宽为400px,基于RGB的 ...

  7. Qt+Python开发百度图片下载器

    一.资源下载地址 https://www.aliyundrive.com/s/jBU2wBS8poH 本项目路径:项目->收费->百度图片下载器(可试用5分钟) 安装包直接下载地址:htt ...

  8. 用python做一个图片验证码

    看一下做出来的验证码长啥样 验证码分析 1. 有很多点 2. 有很多线条 3. 有字母,有数字 需要用到的模块: 1. random 2. Pillow  (python3中使用pillow) 安装p ...

  9. python开发_tkinter_图片操作

    在java的swing中,我们可以找到一些有关图片的操作,对于python的tkinter类似,也有对于图片的相关操作 下面是我做的demo 运行效果: ======================= ...

  10. python+selenium识别图片验证码

    import timeimport pytesseractfrom PIL import Image, ImageEnhancefrom selenium import webdriver url = ...

随机推荐

  1. 使用Base SDK 6.1编译的APP在iOS7的设备上运行,NavigationBar覆盖view的解决办法

    if (__IPHONE_OS_VERSION_MAX_ALLOWED <= __IPHONE_6_1) { self.navigationController.navigationBar.tr ...

  2. bat、cmd、dos窗口:后台调用,不显示黑色的控制台dos(命令行)窗口

    建立一个windows的vbs脚本文件,内容类似如下:注意末尾的参数0 createobject("wscript.shell").run "VBoxheadless.e ...

  3. VC++ 创建一个动态增长的层叠菜单

    工作中需要创建一个动态增长的层叠菜单,类似于动态增长的多语言切换菜单,也是废了好大劲哪,分享一下,请交流参考. 类似效果图: 弹出子菜单各菜单项的意义一致,用ON_COMMAND_RANGE宏来统一实 ...

  4. 通过notepad++将混乱的xml配置的格式进行美化

    需求描述: 最近在进行hbase配置文件的修改之后,发现xml文件的格式很不美观, 然后,在网上找了些方法,实测,通过notepad++的xml tools插件就可 达到美化效果. 操作过程: 1.以 ...

  5. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFacto

    控制台: 2016-4-1 16:32:06 org.hibernate.annotations.common.Version <clinit> 信息: Hibernate Commons ...

  6. Linux环境PHP5.5以上连接SqlServer2008

    linux版本:64位CentOS 6.4 Nginx版本:nginx1.8.0 php版本:php5.5.28 Sqlserver版本:2008 FreeTDS版本:0.95 关于Linux环境安装 ...

  7. python2.0_s12_day14_jQuery详解

    jquery的中文介绍文档:http://www.php100.com/manual/jquery/jQuery之基本选择器jQuery中提供的用于获取标签的方法都有哪些? jQuery提供的 &qu ...

  8. join()函数的用法【python】

    转自:http://www.jb51.net/article/63598.htm

  9. 也谈谈Unity的transform使用

    一.Transform和transform 我们来详谈Unity的transform使用,这里所说的tansform不是类UnityEngine命名空间下的Transform,而是transform. ...

  10. Jquery checkbox选中问题

    checkbox中有.checked的写法,判断当前是否是选中状态,不过这种是针对[object HTMLInputElement]这种类型的,而对于[object Object]这种类型是不能使用的 ...