Python server-side SDK integration
This topic describes how to integrate the Python server-side SDK.
Prerequisites
You have an AccessKey for your Alibaba Cloud account. For more information, see Create an AccessKey.
You have downloaded the Python server-side SDK package from the Alibaba Cloud Captcha console.
Your server-side development environment uses Python 2.7 or a later version.
Install the SDK
Decompress the downloaded python_sdk.zip package to any folder on the server. Ensure that the server has read permission for the package.
Notes
The classes for the Alibaba Cloud Captcha server-side SDK are in the com.aliyuncs.IAcsClient package.
The default connection timeout for SDK interface calls is 3 seconds and the read timeout is 80 seconds. You can call the constructor with the connectTimeout and readTimeout parameters to customize the connection and read timeouts.
Initialize IClientProfile
IClientProfile is the interface used to interact with the Alibaba Cloud Captcha server. All SDK operations use IClientProfile.
IClientProfile is reusable. Set it as a globally unique instance for your application.
from aliyunsdkcore import client
from aliyunsdkafs.request.v20180112 import AuthenticateSigRequest
from aliyunsdkcore.profile import region_provider
region_provider.add_endpoint('afs', 'cn-hangzhou', 'afs.aliyuncs.com')
# Replace YOUR ACCESSKEY and YOUR ACCESS_SECRET with your Alibaba Cloud AccessKey ID and AccessKey secret.
clt = client.AcsClient('YOUR ACCESSKEY', 'YOUR ACCESS_SECRET', 'cn-hangzhou') Call the Alibaba Cloud Captcha server-side interface
After you initialize IClientProfile, call the AnalyzeNvcRequest interface. You also need to develop a handler class for frontend page requests and a method to process the operation results.
request = AnalyzeNvcRequest.AnalyzeNvcRequest()
# Required. The value obtained from the frontend by the getNVCVal method.
request.set_Data('xxx')
# Use the set_ScoreJsonStr method to declare the mapping between the server-side response and the frontend action, and to notify the Captcha server to authorize secondary authentication.
# Note: The frontend page must strictly follow this mapping to perform operations. Otherwise, the call fails.
# For example, if you declare "400":"NC" in the set_ScoreJsonStr method, your frontend must trigger the slide-to-verify challenge (NC) when the server returns 400. If another type of challenge is triggered, the process fails.
request.set_ScoreJsonStr('{"200":"PASS","400":"NC","800":"BLOCK"}');
result = clt.do_action(request)
print result