Python对接
Python对接网络验证
本文档介绍如何在 Python 环境中使用 Kauth SDK 实现网络验证功能。
1. 准备工作
1.1 下载 SDK
重要提示 首先下载SDK文件: 网盘下载
Python SDK 包含以下文件:
kauth.py- SDK 核心文件httpResp.py- 响应数据模型- 需要安装的依赖:
requests,pycryptodome,rsa
1.2 安装依赖
安装必要的依赖包:
pip install requests pycryptodome rsa
1.3 获取必要的参数
在使用 SDK 之前,你需要从 Kauth 管理后台获取以下信息:
- API 域名(
apiDomain):验证服务器地址 - 程序ID(
programId):登录后在"程序管理->程序列表"中查看 - 程序密钥(
programSecret):登录后在"程序管理->程序列表"中查看 - 商户公钥(
merchantPublicKey):登录后在"系统配置"中查看,需要复制 RSA 密钥,注意使用 PKCS#8 格式
1.4 签名类型
Python SDK 目前只支持 RSA 签名算法。
2. 初始化配置
2.1 生成设备ID
设备ID用于标识用户的设备,可以基于系统信息生成:
import hashlib
import platform
import uuid
def generate_system_based_id():
"""基于系统基本信息生成设备ID"""
# 收集系统信息
system_info = {
"node": platform.node(), # 计算机名
"system": platform.system(), # 操作系统
"processor": platform.processor(), # 处理器
"mac": ':'.join(['{:02x}'.format((uuid.getnode() >> elements) & 0xff)
for elements in range(0, 2 * 6, 2)][::-1])
}
# 生成哈希
info_string = ''.join(system_info.values())
device_id = hashlib.md5(info_string.encode()).hexdigest()
return device_id
# 获取设备ID
def getDeviceId():
"""需要开发者依据自己的实际运行环境来生成设备ID"""
# 可以生成一个UUID存储在本地文件,每次启动时读取作为设备ID
return generate_system_based_id()
2.2 初始化 SDK
引入 SDK 并初始化配置:
from kauth import Kauth
# 当前平台类型
platformType = "python_pc"
# 初始化 SDK
kauth = Kauth(
apiDomain="https://verifyhub.cn",
merchantPublicKey="你的商户公钥",
programId="你的程序ID",
programSecret="你的程序密钥",
signType="RSA", # 当前SDK目前只实现了RSA签名算法
deviceId=getDeviceId()
)
3. 登录功能
3.1 卡密登录
使用卡密进行登录:
result = kauth.kaPwdLogin("卡密", platformType, "", "")
print(result.toJson())
# 预期结果:
# {
# "code": "200",
# "msg": "操作成功",
# "success": true,
# "userId": "1982380990267822082",
# "nickName": "卡密用户"
# }
3.2 账号密码登录
使用用户名和密码进行登录:
login_result = kauth.pwdLogin("用户名", "密码", "", "")
print("账号密码登录结果:", login_result.toJson())
# 预期结果:
# {
# "code": 200,
# "msg": "操作成功",
# "success": true,
# "userId": "1973207649463582721",
# "nickName": "test_user"
# }
3.3 试用登录
使用试用功能登录:
trial_login_result = kauth.trialLogin()
print("试用登录结果:", trial_login_result.toJson())
# 预期结果:
# {
# "code": 200,
# "msg": "操作成功",
# "success": true,
# "userId": "1982447320034983937",
# "nickName": "卡密用户"
# }
3.4 退出登录
退出当前登录状态:
loginout_result = kauth.loginOut()
print("退出登录结果:", loginout_result.toJson())
# 预期结果:
# {
# "code": 200,
# "msg": "操作成功",
# "success": true
# }
4. 用户管理
4.1 用户注册
注册新用户:
register_result = kauth.register(
"用户名", # loginName
"密码", # password
"用户昵称", # nickName
"注册卡密", # kaPassword(可选)
"", # captchaCode
"" # captchaUuid
)
print("用户注册结果:", register_result.toJson())
# 预期结果:
# {
# "code": 200,
# "msg": "操作成功",
# "success": true
# }
4.2 修改密码
修改用户密码:
change_password_result = kauth.changePassword(
"用户名", # loginName
"旧密码", # oldPassword
"新密码", # newPassword
"新密码", # confirmPassword
"", # captchaCode
"" # captchaUuid
)
print("修改密码结果:", change_password_result.toJson())
4.3 获取用户信息
获取当前登录用户的信息:
user_info_result = kauth.userInfo()
print("用户信息:", user_info_result.toJson())
# 注册用户信息示例:
# {
# "code": 200,
# "msg": "操作成功",
# "success": true,
# "userId": "1973207649463582721",
# "serverExpireTime": "2025-10-28 22:02:11", # 服务到期时间
# "serverRemainNum": null, # 剩余服务次数
# "serverType": "time", # 服务类型
# "trial": false # 是否试用
# }
# 卡密用户信息示例:
# {
# "code": 200,
# "msg": "操作成功",
# "success": true,
# "userId": "1982447316457242626",
# "serverExpireTime": null,
# "serverRemainNum": "7", # 服务剩余次数
# "serverType": "ci", # 服务类型
# "trial": false
# }
5. 充值功能
5.1 账号充值
使用卡密为账号充值:
recharge_result = kauth.recharge(
"用户名", # loginName
"充值卡密", # kaPassword
"", # captchaCode
"" # captchaUuid
)
print("账号充值结果:", recharge_result.toJson())
# 预期结果:
# {
# "code": 200,
# "msg": "操作成功",
# "success": true
# }
5.2 以卡充卡
使用卡密A为卡密B充值:
recharge_ka_result = kauth.rechargeKa(
"卡密A", # cardPwd
"卡密B" # rechargeCardPwd
)
print("以卡充卡结果:", recharge_ka_result.toJson())
# 预期结果:
# {
# "code": 200,
# "msg": "操作成功",
# "success": true
# }
6. 程序信息
6.1 获取程序详情
获取程序的详细信息(无需登录):
detail = kauth.getProgramDetail()
print("程序详情:", detail.toJson())
# 预期结果:
# {
# "code": 200,
# "msg": "操作成功",
# "success": true,
# "programId": "1959821336266936321",
# "name": "第一个程序",
# "forceUpdateDict": true,
# "status": true,
# "enableRegisterDict": true,
# "notice": "测试账号:test 密码:123456",
# "currentVersion": {
# "versionNo": 1,
# "versionName": "1.0.1",
# "versionDesc": "1.0.1",
# "versionDownUrl": "https://www.baidu.com"
# }
# }
6.2 获取服务器时间
获取服务器当前时间(无需登录):
server_time = kauth.getServerTime()
print("服务器时间:", server_time.toJson())
# 预期结果:
# {
# "code": 200,
# "msg": "操作成功",
# "success": true,
# "serverTimeStr": "2025-10-26 21:58:24",
# "serverTimeMill": "1761487104186",
# "serverTimeSec": "1761487104"
# }
6.3 获取验证码
获取登录验证码(无需登录):
import uuid
captcha_result = kauth.getCaptcha(str(uuid.uuid4()))
print("获取验证码结果:", captcha_result.toJson())
# 预期结果:
# {
# "code": 200,
# "msg": "操作成功",
# "success": true,
# "captchaBase64": "iVBORw0KGgoAAAANSUhEUgAAAHg...",
# "uuid": "040d1cd3-9b8d-4427-a4cd-4836cd387a46"
# }
7. 设备管理
7.1 解绑登录中的设备
解除绑定当前登录的设备(需要先登录):
unbind_result = kauth.unBindLoginDevice()
print("解绑设备结果:", unbind_result.toJson())
# 预期结果:
# {
# "code": 200,
# "msg": "操作成功",
# "success": true
# }
7.2 解绑设备(非登录状态)
在未登录状态下解绑设备:
unbind_device_result = kauth.unbindDevice(
"用户名", # loginName
"密码", # password
"", # captchaCode
"" # captchaUuid
)
print("解绑设备结果:", unbind_device_result.toJson())
8. 配置管理
8.1 卡密配置
卡密用户可以保存自定义配置数据:
# 修改卡密配置
update_result = kauth.updateKaConfig("张三:18岁")
print("修改卡密配置结果:", update_result.toJson())
# 查询卡密配置
ka_config_result = kauth.getKaConfig()
print("查询卡密配置结果:", ka_config_result.toJson())
# 预期结果:
# {
# "code": 200,
# "msg": "操作成功",
# "success": true,
# "config": "张三:18岁"
# }
8.2 用户配置
注册用户可以保存自定义配置数据:
# 修改用户配置
update_result = kauth.updateUserConfig("李四:21岁")
print("修改用户配置结果:", update_result.toJson())
# 查询用户配置
user_config_result = kauth.getUserConfig()
print("查询用户配置结果:", user_config_result.toJson())
# 预期结果:
# {
# "code": 200,
# "msg": "操作成功",
# "success": true,
# "config": "李四:21岁"
# }
9. 远程数据管理
远程数据管理功能允许用户存储和管理自定义数据。
9.1 添加远程数据
add_result = kauth.addRemoteData("user_config_1", "张三")
print("添加远程数据结果:", add_result.toJson())
9.2 获取远程数据
get_result = kauth.getRemoteData("user_config_1")
print("查询远程数据结果:", get_result.toJson())
# 预期结果:
# {
# "code": 200,
# "msg": "操作成功",
# "success": true,
# "value": "张三"
# }
9.3 更新远程数据
update_result = kauth.updateRemoteData("user_config_1", "李四")
print("更新远程数据结果:", update_result.toJson())
9.4 删除远程数据
delete_result = kauth.deleteRemoteData("user_config_1")
print("删除远程数据结果:", delete_result.toJson())
10. 远程变量
获取远程变量(无需登录):
remote_var_result = kauth.getRemoteVar("test_var_to_get")
print("获取远程变量结果:", remote_var_result.toJson())
# 预期结果:
# {
# "code": 200,
# "msg": "操作成功",
# "success": true,
# "value": "123213123123"
# }
11. 远程函数
调用远程函数(需要登录):
# 定义函数参数
function_params = [
{
"paramName": "param1", # 变量名
"paramValue": "12" # 变量值
},
{
"paramName": "param2",
"paramValue": "24"
}
]
# 调用远程函数
function_result = kauth.callFunction("function_1", function_params)
print("调用远程函数结果:", function_result.toJson())
# 预期结果:
# {
# "code": 200,
# "msg": "操作成功",
# "success": true,
# "result": "36.0"
# }
12. 脚本更新
获取并更新最新脚本:
# 查询最新的脚本
newest_result = kauth.getNewestScript("test_script")
print("查询最新脚本结果:", newest_result.toJson())
# 预期结果:
# {
# "code": 200,
# "msg": "操作成功",
# "success": true,
# "scriptName": "test_script",
# "versionNumber": "1.0.1",
# "versionDescription": "新版本1.0.1",
# "scriptType": "TEXT",
# "scriptReleaseTime": "2025-10-01 18:43:21"
# }
# 查询脚本的临时下载地址
if newest_result.success and newest_result.versionNumber:
script_download_result = kauth.getScriptDownload(
"test_script",
newest_result.versionNumber
)
print("脚本下载地址:", script_download_result.toJson())
# 下载脚本内容
if script_download_result.success:
import requests
response = requests.get(script_download_result.downloadUrl)
print("脚本内容 =", response.text)
13. 完整示例
import hashlib
import platform
import uuid
from kauth import Kauth
# 生成设备ID
def generate_system_based_id():
system_info = {
"node": platform.node(),
"system": platform.system(),
"processor": platform.processor(),
"mac": ':'.join(['{:02x}'.format((uuid.getnode() >> elements) & 0xff)
for elements in range(0, 2 * 6, 2)][::-1])
}
info_string = ''.join(system_info.values())
device_id = hashlib.md5(info_string.encode()).hexdigest()
return device_id
def getDeviceId():
return generate_system_based_id()
# 初始化 SDK
platformType = "python_pc"
kauth = Kauth(
apiDomain="https://verifyhub.cn",
merchantPublicKey="你的商户公钥",
programId="你的程序ID",
programSecret="你的程序密钥",
signType="RSA",
deviceId=getDeviceId()
)
# 卡密登录
result = kauth.kaPwdLogin("你的卡密", platformType, "", "")
if result and result.success:
print("登录成功,用户ID:", result.userId)
# 获取用户信息
user_info = kauth.userInfo()
print("用户类型:", user_info.serverType)
print("剩余次数:", user_info.serverRemainNum)
print("到期时间:", user_info.serverExpireTime)
# 使用远程数据
kauth.addRemoteData("my_data", "Hello World")
data = kauth.getRemoteData("my_data")
print("远程数据:", data.value)
# 退出登录
kauth.loginOut()
else:
print("登录失败:", result.msg)
14. 注意事项
SDK 下载:SDK 下载请登录后台管理系统,在系统首页进行下载。
需要登录的功能:所有需要登录的功能,必须先调用登录接口。
设备ID:设备ID需要开发者根据实际运行环境生成,建议生成一个UUID并存储在本地文件,每次启动时读取作为设备ID。
配置存储:卡密配置和用户配置是分别独立的存储空间。
远程功能:远程函数和远程数据需要登录后才能使用。
脚本更新:脚本更新功能可以用于实现脚本在线更新。
验证码:验证码用于某些安全操作,如注册、修改密码等。
响应对象:所有 API 返回都是响应对象,可以使用
.toJson()方法转换为 JSON 格式。
15. 错误处理
所有 API 调用都会返回响应对象,包含统一格式的数据:
class ApiResult:
code: int # 状态码,200表示成功
msg: str # 提示信息
success: bool # 是否成功
建议在实际使用时检查 success 字段:
result = kauth.kaPwdLogin("卡密", platformType, "", "")
if result.success:
# 登录成功
print("用户ID:", result.userId)
else:
# 登录失败
print("登录失败:", result.msg)
16. 更多信息
- 更多 API 接口说明请参考 API 文档
- 如有问题,请联系技术支持:admin@verifyhub.cn 或 QQ: 5027954