This topic describes how to install the environment for using Server Migration Center (SMC) SDK for Python and run the sample code to call an API operation.
Prerequisites
Python 3 is downloaded and installed.
Run the
python --version
command on the terminal to check the Python version.A Python code editor is downloaded.
In this example, PyCharm is used.
An AccessKey pair is obtained.
When you call an API operation, you must use an AccessKey pair to complete identity authentication. An AccessKey pair consists of an AccessKey ID and an AccessKey secret. For more information about how to create an AccessKey pair, see Create an AccessKey pair.
Procedure
Step 1: Configure environment variables
In this example, environment variables are configured to manage the AccessKey pair. This prevents security risks that are caused by hard-coding the AccessKey pair into your business code. For more information, see Configure environment variables in Linux, macOS, and Windows.
Step 2: Install dependencies
Use one of the following methods to install the dependencies:
If pip is not installed, visit the official website of pip to download and install pip. For more information, see Installation.
pip install alibabacloud_credentials
pip install alibabacloud_smc20190601==1.0.2
pip install alibabacloud_tea_console==0.0.1
pip install alibabacloud_tea_util==0.3.11
pip install alibabacloud_tea_openapi==0.3.8
Step 3: Download the sample SDK project
Go to the
API debugging page of OpenAPI Portal. In the left-side navigation pane, select an API operation that you want to call.
In this example, the DescribeSourceServers operation is selected.
In the DescribeSourceServers pane, specify the Region parameter and click the Parameters tab. On the Parameters tab, specify the request parameters. When you specify the request parameters of the API operation, you can refer to the information on the Document tab on the right. On this tab, you can also learn about the limits and usage notes of an API operation, especially the billing information, and the specific meaning and usage of each parameter in the request.
On the SDK Sample Code tab, select Python for the Languages parameter and click Download Project.
NoteFor more information about how to copy the SDK sample code, see Automatic generation of SDK examples.
For more information about the sample code of the SDK for Python, see
Step 4: Import the decompressed project
Open PyCharm.
In the top navigation bar, choose
.In the dialog box that appears, select the decompressed SDK project file.
Step 5: Run the SDK sample code
In the left-side navigation pane of PyCharm, open the SDK project file.
In the top navigation bar, choose
to run the sample code.View the result.
You can also configure an HTTPS request, a proxy, a timeout period, and a retry mechanism in the SDK for Python based on your business requirements. For more information, see Configure an HTTPS request.