Integrate the server-side SDK for Python

更新时间:
复制 MD 格式

This topic describes how to integrate the server-side Captcha software development kit (SDK) for Python.

Prerequisites

  • You have an AccessKey for your Alibaba Cloud account.

  • You have downloaded the server-side SDK package for Python from the Captcha console.

  • Your server-side development environment runs Python 2.7 or later.

Install the SDK

Unzip the downloaded python_sdk.zip package to any folder on your server. Ensure that you have read permissions for the folder.

Notes

  • The classes related to the server-side Captcha SDK are in the com.aliyuncs.IAcsClient package.

  • The default connection timeout for SDK calls is 3 seconds, and the default read timeout is 80 seconds. You can call the constructor with connectTimeout and readTimeout to customize the connection and read timeouts.

Initialize IClientProfile

IClientProfile is the interface that interacts with the Captcha server-side API. All SDK operations are performed using IClientProfile. Because IClientProfile can be reused, you should set it as a globally unique object in 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 Captcha server-side API

After you initialize IClientProfile, you can call the API provided by the Captcha service. You can also develop a handler class for frontend page requests and a method to process the results as needed.

For more information about the Captcha service API, see Captcha service API for web and HTML5 applications.

request = AuthenticateSigRequest.AuthenticateSigRequest()
# The session ID. This is a required parameter. Obtain it from the frontend. Do not change the value.
request.set_SessionId('xxx')
# The signature string. This is a required parameter. Obtain it from the frontend. Do not change the value.
request.set_Sig('xxx')
# The unique request token. This is a required parameter. Obtain it from the frontend. Do not change the value.
request.set_Token('xxx')
# The scenario ID. This is a required parameter. Obtain it from the frontend. Do not change the value.
request.set_Scene('xxx')
# The application key. This is a required parameter. Specify it on the server-side.
request.set_AppKey('xxx')
# The client IP address. This is a required parameter. Specify it on the server-side.
request.set_RemoteIp('xxx')
result = clt.do_action(request)
# A return code of 100 indicates that the signature verification is successful. A return code of 900 indicates that the signature verification failed.
print result