All Products
Search
Document Center

Anycast Elastic IP Address:Use SDK for Python

Last Updated:Sep 14, 2024

This topic uses Python as an example to describe how to use OpenAPI Explorer to create an Anycast elastic IP address (Anycast EIP).

Step 1: View the API references

The List of operations by function topic shows that you can call the AllocateAnycastEipAddress operation to create an Anycast EIP. For more information about the data and permissions that are required to call this operation, see the API documentation.

Step 2: Create a RAM user and grant permissions to the RAM user

Important

An Alibaba Cloud account has access permissions on all API operations. We recommend that you use a Resource Access Management (RAM) user to call specific API operations or perform routine O&M.

You can call the operation by using an Alibaba Cloud account, a RAM user, or a RAM role. For more information about their differences, see Identity.

In this example, a RAM user is used to call the operation.

  1. Create a RAM user.

    1. Log on to the RAM console.

    2. In the left-side navigation pane, choose Identities > Users.

    3. On the Users page, click Create User.

    4. On the Create User page, specify the Logon Name and Display Name parameters, and set the Access Mode parameter to Console Access.

    5. Click OK.

      After you create a RAM user, record the logon name and password of the RAM user. When you call API operations, you must use the RAM user to log on to OpenAPI Explorer.

  2. Grant permissions to the RAM user.

    1. Access the RAM user list.

    2. On the Users page, find the RAM user and click Add Permissions in the Actions column.

    3. Enter AliyunAnycastEIP in the search box and select AliyunAnycastEIPFullAccess.

      The following section describes the permissions on Anycast EIP. For more information about how to create custom permissions, see Authorization.

      • AliyunAnycastEIPReadOnlyAccess: read-only access to Anycast EIPs.

      • AliyunAnycastEIPFullAccess: the permission to manage Anycast EIPs.

    4. Click Grant permissions.

  3. Go to the details page of the RAM user. On the Authentication tab, click Create AccessKey.

    For more information, see Create an AccessKey pair.

    Important

    The AccessKey secret is displayed only during creation. You cannot view the AccessKey secret after you create it. Download the key information as a CSV file and keep it confidential.

Step 3: Prepare the environment

  1. Download and install Python.

    For more information, see Install Python.

  2. Build a Python development environment.

    In this example, PyCharm is installed on Windows. For more information, see Build a Python development environment on Windows.

  3. Configure environment variables.

    Before you call the operation, you need to configure environment variables. This way, access credentials can be read from the environment variables. For more information about how to configure environment variables in Linux, macOS, and Windows, see Configure environment variables in Linux, macOS, and Windows.

    Important

    After you configure the environment variables, restart your development tools or services, such as the integrated development environment (IDE). This ensures that the new settings take effect as expected.

  4. Install environment dependencies.

    Run the following commands on your device to install the SDK package and environment dependencies for Anycast EIP:

    pip install alibabacloud_eipanycast20200309==1.1.2
    pip install alibabacloud_tea_console

Step 4: Download the sample code

  1. Log on to the API Debugging page of OpenAPI Explorer as a RAM user.

  2. Select the Anycast EIP service.

  3. In the left-side navigation pane, click AllocateAnycastEipAddress. By default, Region is set to China (Hangzhou). Set ServiceLocation to international.

  4. Click Initiate Call.

    In the Call Result section, you can view the results including Request Header and Response Header. A status code of 200 indicates that the call is successful.

    在线调试.png

  5. Obtain the sample code of the SDK.

    OpenAPI Explorer provides SDKs for multiple programming languages, such as Java, Go, Python, Node.js, TypeScript, PHP, and C++. You can integrate SDKs into your applications to directly call API operations. SDKs encapsulate various information, including the data signing logic, timeout mechanism, and retry mechanism. SDKs return structured response objects based on specifications to facilitate development.

    The sample code of the SDK for Python is used as an example. You can obtain the code for other programming languages in the same way.

    1. On the SDK Sample Code tab, select an SDK version and language. We recommend that you select V2.0. For information about the differences between V2.0 and V1.0, see Alibaba Cloud SDK V1.0 and V2.0.

    2. Click Download Project to download the complete SDK project. Decompress the project after the project is downloaded.

      Note
      • In the upper-right corner of the code editor, you can click the image icon to copy the SDK sample code.

      • You can click SDK installation information to obtain basic SDK information such as the SDK package name, SDK package version, SDK package management platform, and SDK installation command for integration and development.

      获取示例代码.png

Step 5: Run the SDK sample code

  1. Open PyCharm, click File > Open, select the decompressed project file, click OK in the Creating Virtual Environment window, and wait for the Python virtual environment to be created and dependencies to be downloaded.

  2. Run the sample code.

    Double-click sample.py in the file directory alibabacloud_sample on the left side of the PyCharm window, and then click the Run button at the top to view the results.

    If "statusCode":200 is returned, the call is successful.

    运行SDK示例代码.png