Integrate the server-side SDK for Python

更新时间:
复制 MD 格式

Prerequisites

  • You have an AccessKey for your Alibaba Cloud account.

  • You have downloaded the server-side software development kit (SDK) package for Python from the Alibaba Cloud Captcha console.

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

Install the SDK

Extract the contents of the downloaded python_sdk.zip package to a folder on your server. Ensure that you have read permissions for this folder.

Notes

  • The classes for the Alibaba Cloud Captcha server-side SDK are located in the com.aliyuncs.IAcsClient package.

  • The default connection timeout for SDK API calls is 3 seconds, and the read timeout is 80 seconds. To customize these values, you can call the constructor with the connectTimeout and readTimeout parameters.

Initialize IClientProfile

The IClientProfile interface is used to interact with the Alibaba Cloud Captcha server-side API. All SDK operations are performed using IClientProfile.

Note

The IClientProfile object is reusable. We recommend that you declare 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 Alibaba Cloud Captcha server-side API

After you initialize IClientProfile, you can call the Alibaba Cloud Captcha service APIs. You can then develop classes to handle frontend requests and process the results based on your business needs.

For more information about the Alibaba Cloud Captcha service APIs, see Alibaba Cloud Captcha service APIs for web and HTML5 applications.

request = AuthenticateSigRequest.AuthenticateSigRequest()
# Session ID. Required. Obtain this from the frontend. Do not change this value.
request.set_SessionId('xxx')
# Signature string. Required. Obtain this from the frontend. Do not change this value.
request.set_Sig('xxx')
# Unique request ID. Required. Obtain this from the frontend. Do not change this value.
request.set_Token('xxx')
# Scenario ID. Required. Obtain this from the frontend. Do not change this value.
request.set_Scene('xxx')
# Application type ID. Required. Specify this on the backend.
request.set_AppKey('xxx')
# Client IP address. Required. Specify this on the backend.
request.set_RemoteIp('xxx')
result = clt.do_action(request)
# A return code of 100 means the signature is verified. A return code of 900 means the verification failed.
print result