1.下载和安装验证码模块包
git clone https://github.com/GeeTeam/gt3-python-sdk.git
2.安装依赖模块
pip install requests -i https://pypi.douban.com/simple
3.使用极验验证技术
# Create your views here.
from rest_framework.views import APIView
from luffyapi.libs.geetest import GeetestLib
from rest_framework.response import Response
from django.conf import settings
class GeetestCaptchaAPIView(APIView):
"""极验验证码视图类"""
def __init__(self):
super().__init__()
self.gt = GeetestLib(settings.GEETEST["pc_geetest_id"], settings.GEETEST["pc_geetest_key"])
self.user_id = "test"
self.status = False
def get(self,request):
"""获取验证码"""
self.status = self.gt.pre_process(self.user_id)
# 将来可以使用redis来保存status和user_id
response_str = self.gt.get_response_str()
return Response(response_str)
def post(self,request):
"""验证码二次验证"""
challenge = request.data.get(self.gt.FN_CHALLENGE, '')
validate = request.data.get(self.gt.FN_VALIDATE, '')
seccode = request.data.get(self.gt.FN_SECCODE, '')
if self.status:
result = self.gt.success_validate(challenge, validate, seccode, self.user_id)
else:
result = self.gt.failback_validate(challenge, validate, seccode)
result = {"status": "success"} if result else {"status": "fail"}
return Response(result)
4.在settings.dev下设置配置(这个配置是为了以后方便调整,统一管理的作用)
# 极验验证码
GEETEST = {
"pc_geetest_id": "e951ba436ca7c3f6192615f006d0b2ae",
"pc_geetest_key": "fba7357abd64ff84132bd520834afe4e",
}
#极验验证码,每个人的id 和key都是不一样的,千万不要直接使用