前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python实现生成验证码的逻辑

python实现生成验证码的逻辑

作者头像
V站CEO-西顾
发布2018-06-17 12:07:48
7010
发布2018-06-17 12:07:48
举报
文章被收录于专栏:V站V站

V站笔记

 假设我们有一个fonts的文件夹,里面有1.ttf,2.ttf,3.ttf三个字体文件

具体代码实现代码codes.py:

  1. # coding:utf8
  2. from PIL import ImageDraw, ImageColor, ImageFile, ImageFont, ImageFilter, Image
  3. import random
  4. import os
  5. import uuid
  6. class Codes:
  7. # 定义随机字符
  8. def random_chr(self):
  9.         num = random.randint(1, 3)
  10. if num == 1:
  11.             char = random.randint(48, 57)
  12. elif num == 2:
  13.             char = random.randint(97, 122)
  14. else:
  15.             char = random.randint(65, 90)
  16. return chr(char)
  17. # 定义干扰字符
  18. def random_dis(self):
  19.         arr = ["~", "^", "_", "."]
  20. return arr[random.randint(0, len(arr) - 1)]
  21. # 定义干扰字符的颜色
  22. def random_color1(self):
  23. return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255))
  24. def random_color2(self):
  25. return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127))
  26. # 生成验证码
  27. def create_code(self):
  28.         width = 200
  29.         height = 60
  30. # 创建一个画布图片
  31.         image = Image.new("RGB", (width, height), (192, 192, 192))
  32. # 创建font对象,定义字体以及大小
  33.         font_name = random.randint(1, 3)
  34.         font_file = os.path.join(os.path.dirname(__file__), "fonts") + "/" + "%d.ttf" % font_name
  35.         font = ImageFont.truetype(font_file, 30)
  36. # 填充像素点
  37.         draw = ImageDraw.Draw(image)
  38. for x in range(0, width, 5):
  39. for y in range(0, height, 5):
  40.                 draw.point((x, y), fill=self.random_color1())
  41. # 填充干扰字符
  42. for v in range(0, width, 30):
  43.             dis = self.random_dis()
  44.             w = 5 + v
  45.             h = random.randint(5, 15)
  46.             draw.text((w, h), dis, font=font, fill=self.random_color1())
  47. # 填充字符
  48.         chars = ""
  49. for v in range(4):
  50.             c = self.random_chr()
  51.             chars += str(c)
  52.             h = random.randint(5, 15)
  53.             w = width / 4 * v + 10
  54.             draw.text((w, h), c, font=font, fill=self.random_color2())
  55. # 模糊效果
  56.         image = filter(ImageFilter.BLUR)
  57. # 保证图片名称的唯一性
  58.         image_name = "%s.jpg" % uuid.uuid4().hex
  59.         save_dir = os.path.join(os.path.dirname(__file__), "codes")
  60. if not os.path.exists(save_dir):
  61. os.makedirs(save_dir)
  62.         image.save(save_dir + '/' + image_name)
  63. return dict(
  64.             img_name=image_name,
  65.             code=chars
  66.         )

 应用

flask项目中

  • 在定义路由的views.py文件中引用。自定义一个"/codes/"的路由,用来GET到验证码
  1. # 验证码
  2. @app.route('/codes/', methods=["GET"])
  3. def codes():
  4. from codes import Codes
  5.     c = Codes()
  6.     info = c.create_code()
  7.     image = os.path.join(os.path.dirname(__file__), "static/code") + "/" + info["img_name"]
  8.     with open(image, "rb") as f:
  9.         image = f.read()
  10.     session["code"] = info["code"]
  11. return Response(image, mimetype="jpeg")

上面读取到图片并显示出来了,将验证码的值给到session["code"]来存储,这样访问http:xxx.xx.xx./codes/就可以获取到验证码的图片,并保存在本地code的文件夹中

  • 然后在前端页面验证码图片显示的的地方引用,点击图片会重新切换生成一个
  1. <img alt="点击切换验证码" title="点击切换" src="/codes/" onclick="this.src='/codes/?' + Math.random()"
  2. style="width: 180px; height:50px; margin-top: 6px;">
  • 在forms.py文件中存入session的code与code表单的输入的值进行对比判断
  1. # 自定义验证码验证功能
  2. def validate_code(self,field):
  3.         code = field.data
  4. # if not session.has_key("code"): python2写法
  5. if "code" not in session:
  6. raise ValidationError("没有验证码")
  7. # if session.has_key("code") and session["code"].lower() != code.lower():python2写法
  8. if "code" in session and session["code"].lower() != code.lower(): # 从views中获取session的"code"
  9. raise ValidationError("验证码错误")

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  •  应用
相关产品与服务
验证码
腾讯云新一代行为验证码(Captcha),基于十道安全栅栏, 为网页、App、小程序开发者打造立体、全面的人机验证。最大程度保护注册登录、活动秒杀、点赞发帖、数据保护等各大场景下业务安全的同时,提供更精细化的用户体验。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档