吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1914|回复: 7
收起左侧

[Python 转载] python web验证码异步刷新实现

[复制链接]
lz270978971 发表于 2020-5-18 19:36
此代码是需要依赖:
sanic==19.9.0
Pillow==7.0.0
[Python] 纯文本查看 复制代码
import random
import string
import uuid
import base64
import platform
from PIL import Image, ImageDraw,ImageFont
from io import BytesIO
from sanic import Sanic
from sanic.response import HTTPResponse,text, json
from sanic.views import HTTPMethodView

app = Sanic()

session = {}


class VerifyCode:
    def __init__(self, numbers:int):
        """
        指定:生成的数量
        """
        self.number = numbers

    def draw_lines(self, draw, num, width, height):
        """划线"""

        x1 = random.randint(0, width / 2)
        y1 = random.randint(0, height / 2)
        x2 = random.randint(0, width)
        y2 = random.randint(height / 2, height)
        draw.line(((x1, y1), (x2, y2)), fill='black', width=1)

    def random_color(self):
        """随机颜色"""
        return random.randint(32, 127), random.randint(32, 127), random.randint(32, 127)

    def gene_text(self):
        """生成验证码"""
        return "".join(random.sample(string.ascii_letters+string.digits, self.number))

    def get_verify_code(self):
        """
        draw.text():
            文字的绘制,第一个参数指定绘制的起始点(文本的左上角所在位置),第二个参数指定文本内容,第三个参数指定文本的颜色,第四个参数指定字体(通过ImageFont类来定义)
        """
        code = self.gene_text()
        width, height = 130, 30
        im = Image.new("RGB", (width, height), "white")
        # 这里指定字体的路径
        sysstr = platform.system()
        font = None
        if sysstr == "Windows":
            font = ImageFont.truetype("C:\WINDOWS\Fonts\STXINGKA.TTF", 25)
        elif sysstr == "Darwin":
            font = ImageFont.truetype('/Library/Fonts/AppleMyungjo.ttf', 25)
        draw = ImageDraw.Draw(im)
        for item in range(self.number):
            draw.text((5+random.randint(-5, 5)+23*item, 5+random.randint(-5, 5)), text=code[item],
                      fill=self.random_color(), font=font)
            self.draw_lines(draw, self.number, width, height)
        return im, code


class SimpleView(HTTPMethodView):
    body = """
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>登录</title>
    <link rel="stylesheet"  integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <!-- 可选的 Bootstrap 主题文件(一般不用引入) -->
    <link rel="stylesheet"  integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</head>
<body>
<form class="form-inline" method="post" action="">
    <div>
         <div class="form-group">
                            {error}                               
        </div>
        <div class="form-group">
            <label for="exampleInputName2">验证码</label>
           <input type="text" class="form-control" id="captcha" name="code">
        </div>
        <div class="form-group">
            <img src="data:image/jpeg;base64,{base64_data}" class="img-img-rounded" id="img">
        </div>
        <div class="form-group">
            <button type="submit" id="submit">验证</button>
        </div>
    </div>
</form>
</body>
<script type="text/javascript">
jQuery(document).ready(function(){{
    $("#img").click(function(){{
        $.post("",
        {{
            type:"put"
        }},
        function(data){{
            var image = data["result"]["base64_data"]
            var arr=new Array();
            arr.push("data:image/jpeg;base64,")
            arr.push(image)
            var h = arr.join("")
            $("#img").attr("src", h);
        }});
    }});
}});
</script>
</html>
    """

    async def get(self, request):
        return self.response(id="", error="")

    async def post(self, request):
        uuid = request.cookies.get("uuid", "")
        method = request.form.get("type", "")
        if method:
            base64_data = self.put(request)
            return json({"status": "success", "result": {"base64_data": base64_data}})
        verfy_code = request.form.get("code", "2").lower()
        code = session.get(uuid, "").lower()
        if code == verfy_code:
            return text('验证码正确')
        return self.response(id=uuid, error='<input class="form-control" id="disabledInput" type="text" placeholder="验证码错误" disabled>')

    def put(self, request):
        uuid = request.cookies.get("uuid", "")
        base64_data, code = self.code()
        session[uuid] = code
        return base64_data

    def code(self):
        im, code = VerifyCode(5).get_verify_code()
        buf = BytesIO()
        im.save(buf, "jpeg")
        buf_str = buf.getvalue()
        base64_data = base64.b64encode(buf_str).decode()
        return base64_data, code

    def response(self, id, error):
        id = id if id else uuid.uuid1().__str__()
        base64_data, code = self.code()
        session[id] = code
        body = self.body.format(base64_data=base64_data, error=error)
        response = HTTPResponse(body, content_type="text/html; charset=utf-8")
        response.cookies["uuid"] = id
        return response


app.add_route(SimpleView.as_view(), '')

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8000)


QQ20200518-193546.gif

免费评分

参与人数 1吾爱币 +5 热心值 +1 收起 理由
苏紫方璇 + 5 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

深爱我的女孩 发表于 2020-5-18 19:54
不懂,帮顶!
 楼主| lz270978971 发表于 2020-5-18 19:39
MOEYU_VANILLA 发表于 2020-5-18 21:37
zucker 发表于 2020-5-19 10:26
拿来做啥
 楼主| lz270978971 发表于 2020-5-20 21:21

做web验证码呗
zucker 发表于 2020-5-21 13:36

不直接找个已有的轮子api
hf11001 发表于 2020-5-25 10:28
你好,可以讲些下思路就更好了
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-17 06:54

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表