极验验证使用-滑动&选字验证码
2024-10-18 19:23:25
准备
SDK下载
首先在极验官网下载好SDK,附上官网链接,点此可直接下载python版zip包。
模块安装
使用该SDK时发现它依赖两个模块,分别是geetest和requests。
pip install geetest pip install requests
使用
我这里是在Django环境下测试。
登录页
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登陆</title> <!-- 为使用方便,直接使用jquery.js库,如您代码中不需要,可以去掉 --> <script src="http://code.jquery.com/jquery-1.12.3.min.js"></script> <!-- 引入封装了failback的接口--initGeetest --> <script src="http://static.geetest.com/static/tools/gt.js"></script> </head> <body> <div class="popup"> <br> <p> <labe>用户名:</labe> <input id="username1" class="inp" type="text" value="admin"> </p> <br> <p> <label>密 码:</label> <input id="password1" class="inp" type="password" value="123"> </p> <br> <input class="btn" id="popup-submit" type="submit" value="提交"> <div id="popup-captcha"></div> </div> <script> var handlerPopup = function (captchaObj) { // 成功的回调 captchaObj.onSuccess(function () { var validate = captchaObj.getValidate(); $.ajax({ url: "/login/", // 进行二次验证 type: "post", dataType: "json", data: { username: $('#username1').val(), password: $('#password1').val(), geetest_challenge: validate.geetest_challenge, geetest_validate: validate.geetest_validate, geetest_seccode: validate.geetest_seccode }, success: function (data) { alert(data.msg) } }); }); $("#popup-submit").click(function () { captchaObj.show(); }); // 将验证码加到id为captcha的元素里 captchaObj.appendTo("#popup-captcha"); // 更多接口参考:http://www.geetest.com/install/sections/idx-client-sdk.html }; // 验证开始需要向网站主后台获取id,challenge,success(是否启用failback) $.ajax({ url: "/getcaptcha?t=" + (new Date()).getTime(), // 加随机数防止缓存 type: "get", dataType: "json", success: function (data) { // 使用initGeetest接口 // 参数1:配置参数 // 参数2:回调,回调的第一个参数验证码对象,之后可以使用它做appendTo之类的事件 initGeetest({ gt: data.gt, challenge: data.challenge, product: "popup", // 产品形式,包括:float,embed,popup。注意只对PC版验证码有效 offline: !data.success // 表示用户后台检测极验服务器是否宕机,一般不需要关注 // 更多配置参数请参见:http://www.geetest.com/install/sections/idx-client-sdk.html#config }, handlerPopup); } }); </script> </body> </html>
/templates/login.html
注意:需要引入以下js:
<script src="http://code.jquery.com/jquery-1.12.3.min.js"></script> <script src="http://static.geetest.com/static/tools/gt.js"></script>
相应接口
from django.shortcuts import render, HttpResponse from django.http import JsonResponse from geetest import GeetestLib pc_geetest_id = "b46d1900d0a894591916ea94ea91bd2c" pc_geetest_key = "36fc3fe98530eea08dfc6ce76e3d24c4" def getcaptcha(request): user_id = 'test' gt = GeetestLib(pc_geetest_id, pc_geetest_key) status = gt.pre_process(user_id) request.session[gt.GT_STATUS_SESSION_KEY] = status request.session["user_id"] = user_id response_str = gt.get_response_str() return HttpResponse(response_str) # Create your views here. def login(request): if request.method == "POST": gt = GeetestLib(pc_geetest_id, pc_geetest_key) challenge = request.POST.get(gt.FN_CHALLENGE, '') validate = request.POST.get(gt.FN_VALIDATE, '') seccode = request.POST.get(gt.FN_SECCODE, '') status = request.session[gt.GT_STATUS_SESSION_KEY] user_id = request.session["user_id"] if status: result = gt.success_validate(challenge, validate, seccode, user_id) else: result = gt.failback_validate(challenge, validate, seccode) username = request.POST.get('username') password = request.POST.get('password') if result: # 验证成功 ': result = {'status': 0, 'msg': "登录成功"} else: result = {'status': 1, 'msg': "用户名或密码错误"} else: result = {'status': 2, 'msg': "验证失败"} return JsonResponse(result) return render(request, 'login.html') views.py
views.py
运行
配置好路由,运行。访问localhost:8000/login/,点击提交。效果如下图:
选字验证码
在我测试C#版Demo的时候发现它的验证码是选字验证码,最后发现只要把C#版Demo中的id和key替换上述views.py中的5、6行的id和key,页面就是选字验证码。id和key如下:
pc_geetest_id = "48a6ebac4ebc6642d68c217fca33eb4d" pc_geetest_key = "4f1c085290bec5afdc54df73535fc361"
如下图:
完整示例下载
最新文章
- [转]CSS hack大全&;详解
- Objdump-查看汇编指令
- 【CC评网】2013.第42周 话说时间管理
- Amazium源码分析:(1)基本介绍
- jquery easyui的layout
- js函数对象
- SVG的基础使用
- 简单五子棋,没有电脑AI
- 对SVD奇异值分解的理解
- 关于JAVA字符编码:Unicode,ISO-8859-1,GBK,UTF-8编码及相互转换
- c# 图片加水印
- JavaScript中函数引用调用和函数直接调用的区别
- Lost connection to MySQL server during query,MySQL设置session,global变量及网络IO与索引
- Create AI Guard Class
- C++ leetcode::Reverse Integer
- Blender 界面操作
- CRC32是什么?
- VC++开机自动启动程序的几种方法 (转载)
- MySQL5.7.10配置和使用
- 打包python为可执行文件时报错R6034解决方案