All Products
Search
Document Center

Server Migration Center:Use SMC SDK for Python

Last Updated:Aug 01, 2023

This topic describes how to call API operations in OpenAPI Portal to obtain and use the sample code of Server Migration Center (SMC) SDK for Python.

Prerequisites

  • Python 3 is downloaded and installed.

  • A Python code editor is downloaded.

    In this example, PyCharm is used.

  • An AccessKey pair is obtained.

    When you call the API operations, you must use an AccessKey pair to complete identity verification. An AccessKey pair consists of an AccessKey ID and an AccessKey secret. For more information about how to create and obtain an AccessKey pair, see Create an AccessKey pair.

Procedure

Step 1: Download the complete SDK project

  1. 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.

  2. 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.

  3. On the SDK Sample Code tab, select Python for the Languages parameter and click Download Project.

    Note
    image.png

Step 2: Import the decompressed project

  1. Open PyCharm.

  2. In the top navigation bar, choose File > New > Project from Existing Sources....

    image.png
  3. In the dialog box that appears, select the decompressed SDK project file.

Step 3: Configure credentials

Configure the credentials that are required when you call the API operations of Alibaba Cloud services. You can define the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET to configure the default credentials. When you call an API operation, the system reads the AccessKey pair from the default credentials and uses the AccessKey pair to complete authentication.

You can use multiple configuration methods to configure credentials. For more information, see Configure credentials.

  1. In PyCharm Terminal, run the following command to install the Alibaba Cloud SDK Credentials package.

    Note

    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
  2. Configure the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET.

    Note

    For more information about how to create and obtain an AccessKey pair, see Create an AccessKey pair.

    Linux and macOS

    1. Open the terminal in IntelliJ IDEA.

    2. Run the following commands to configure the environment variables.

      Replace <access_key_id> with your AccessKey ID and <access_key_secret> with your AccessKey secret in the following command.

      export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id>
      export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>

    Windows

    1. Create an environment variable file, add the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET to the file, and then specify your AccessKey ID for ALIBABA_CLOUD_ACCESS_KEY_ID and your AccessKey secret for ALIBABA_CLOUD_ACCESS_KEY_SECRET. Restart the Windows operating system. In this example, Windows 10 is used.

    2. Open the File Explorer. Right-click This PC and click Properties.

    3. In the left-side navigation pane, click Advanced System Settings.

    4. In the System Properties dialog box, click the Advanced tab and click Environment Variables.

    5. In the Environment Variables dialog box, click New....

    6. In the New System Variable dialog box, add the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET. Then, enter the AccessKey ID and AccessKey secret.

    7. Restart the Windows operating system.

Step 4: Run the SDK sample code

  1. In the left-side navigation pane of PyCharm, open the SDK project file.

  2. In the top navigation bar, choose Run > Run... to run the sample code.

    image.png
  3. View the result.

    image.png

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.