Python实现图形验证码

  • 格式:docx
  • 大小:241.62 KB
  • 文档页数:4

Python实现图形验证码

1、前言

验证码通常是为了区分用户是人还是计算机,也可以防止解开密码等恶意行为,而客户端上多数会用在关键操作上,比如购买、登录、注册等场景。现在验证码的种类样式也特别多,本文内容为如何用Python做出滑动拼图验证码。

2、前端代码

载入中 ...

3、Python代码

from wsgiref.simple_server import make_server

from KgCaptchaSDK import KgCaptcha

def start(environ, response):

# 填写你的 AppId,在应用管理中获取

AppID = "AppId"

# 填写你的 AppSecret,在应用管理中获取

AppSecret = "AppSecret"

request = KgCaptcha(AppID, AppSecret)

# 填写应用服务域名,在应用管理中获取

request.appCdn = "appCdn "

# 请求超时时间,秒

request.connectTimeout = 10

# 用户id/登录名/手机号等信息,当安全策略中的防控等级为3时必须填写

erId = "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()

4、效果如下

5、最后

以上就是Python实现KgCaptcha图形验证码的内容。