ITPub博客

首页 > 应用开发 > Python > 很漂亮的Python验证码(记录)

很漂亮的Python验证码(记录)

原创 Python 作者:Kyger 时间:2023-03-10 09:39:42 0 删除 编辑

先发张截图看一下效果:

undefined

undefined

undefined


一、前端接入代码:

<script src=" 
<script>
kg.captcha({
    // 绑定元素,验证框显示区域
    bind: "#captchaBox",
        
    // 验证成功事务处理
    success: function(e) {
        console.log(e);
    },
    
    // 验证失败事务处理
    failure: function(e) {
        console.log(e);
    },
    
    // 点击刷新按钮时触发
    refresh: function(e) {
        console.log(e);
    }
});
</script>
<div id="captchaBox"></div>


二、Python 接入代码:

from wsgiref.simple_server import make_server
from KgCaptchaSDK import KgCaptcha
def start(environ, response):
    # 填写你的 AppId,在应用管理中获取
    AppID = "L001"
    # 填写你的 AppSecret,在应用管理中获取
    AppSecret = "W68oJi0iqT2C3BFRGirO1IaYCDvsYEED"
    request = KgCaptcha(AppID, AppSecret)
    # 填写应用服务域名,在应用管理中获取
    request.appCdn = "
    # 请求超时时间,秒
    request.connectTimeout = 10
    # 用户id/登录名/手机号等信息,当安全策略中的防控等级为3时必须填写
    request.userId = "kgCaptchaDemo"
    # 使用其它 WEB 框架时请删除 request.parse,使用框架提供的方法获取以下相关参数
    parseEnviron = request.parse(environ)
    # 前端验证成功后颁发的 token,有效期为两分钟
    request.token = parseEnviron["post"].get("kgCaptchaToken", "")  # 前端 _POST["kgCaptchaToken"]
    # 客户端IP地址
    request.clientIp = parseEnviron["ip"]
    # 客户端浏览器信息
    request.clientBrowser = parseEnviron["browser"]
    # 来路域名
    request.domain = parseEnviron["domain"]
    # 发送请求
    requestResult = request.sendRequest()
    if requestResult.code == 0:
        # 验证通过逻辑处理
        html = "验证通过"
    else:
        # 验证失败逻辑处理
        html = f"{requestResult.msg} - {requestResult.code}"
    
    response("200 OK", [("Content-type", "text/html; charset=utf-8")])
    return [bytes(str(html), encoding="utf-8")]
httpd = make_server("0.0.0.0", 8088, start)  # 设置调试端口  
httpd.serve_forever()


三、源码下载地址:

SDK开源地址:github.com/KgCaptcha,顺便做了一个演示:  w w w.kgcaptcha.com/demo/


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70027530/viewspace-2938977/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2023-03-10

  • 博文量
    15
  • 访问量
    10261