DDR爱好者之家 Design By 杰米

验证码制作

#string模块自带数字、字母、特殊字符变量集合,不需要我们手写集合
import string
import random
import os
import uuid

import settings
from PIL import Image, ImageDraw, ImageColor, ImageFilter, ImageFont


class Code(object):
 # 生成随机生成数字或字母
 def random_hexdigits(self, len=1):
 return random.sample(string.hexdigits, len)
 
 # 生成干扰字符
 def punctuation(self, len=1):
 return tuple(random.sample(string.punctuation, len))
 
 # 定义干扰字符颜色
 def random_color(self, min=64, max=255):
 return tuple((random.randint(min, max) for i in range(3)))
 
 # 生成验证码
 def creat_code(self, width=80, height=24, color=(192, 192, 192)):
 image = Image.new('RGB', (width, height), color)
 #建议下载几款字体,变换下风格,我在setting粒定义了static路径,这里就直接导入了
 font = ImageFont.truetype(os.path.join(settings.STATICPATH, 'fonts/Lora-Regular.ttf'), 20)
 draw = ImageDraw.Draw(image)
 self.fill_color(draw, image, 5)
 self.fill_dischar(draw, image, 10)
 code = self.fill_char(draw, image, 4, 10, font)
 image_name = '{}.jpeg'.format(uuid.uuid4().hex)
 image_path = os.path.join(settings.STATICPATH, 'code/{}'.format(image_name))
 print(image_path)
 image.save(image_path)
 return {'code': code, 'image_path': image_path}
 
 # 填充颜色
 def fill_color(self, draw, image, interval):
 for i in range(0, image.width, interval):
  for j in range(0, image.height, interval):
  draw.point((i, j), fill=self.random_color())
 
 # 填充验证码
 def fill_dischar(self, draw, image, interval):
 for i in range(0, image.width, interval):
  dis = self.punctuation()
  j = random.randrange(3, image.height - 3)
  draw.text((i, j), dis[0], fill=self.random_color(64, 255))
 
 # 填充验证码
 def fill_char(self, draw, image, num, interval, font):
 code = ''
 for i in range(num):
  cha = self.random_hexdigits()
  code += str(cha[0])
  j = random.randrange(0, 5)
  # print(cha)
  # print(image.width*(i/num)+interval,j)
  draw.text((image.width * (i / num) + interval, j), cha[0], fill=self.random_color(32, 127), font=font)
 return code


if __name__ == "__main__":
 code = Code()
 print(code.creat_code())

flask路由配置

import os
from flask import Flask, Response
from flask import render_template
from utils.code import Code

app = Flask(__name__)


@app.route('/')
def Register():
 return render_template('verify.html')


@app.route('/codes/')
def code():
 infor = Code().creat_code()
 image_path = infor["image_path"]
 code = infor['code']
 
 print(image_path)
 with open(image_path, 'rb') as f:
  image_content = f.read()
 os.remove(image_path)
 return Response(image_content, mimetype='jpeg')


if __name__ == '__main__':
 app.run(debug=True)

前端配置

 <div class='form-row'>
   <form id="email_register_form" method="post" autocomplete="off">
    <div class="form-group ">
     <label>邮&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;箱</label>
     <input type="text" id="id_email" name="email" value="None" placeholder="请输入您的邮箱地址"/>
    </div>
    <div class="form-group ">
     <label>密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码</label>
     <input type="password" id="id_password" name="password" value="None" placeholder="请输入6-20位非中文字符密码"/>
    </div>
    <div class="form-group captcha1 ">
     <label>验&nbsp;证&nbsp;码</label>
     <img src="/UploadFiles/2021-04-08/">

源码分享:https://github.com/geekdick/pythonDemo/tree/master/verify

DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?