The Alibaba Cloud SDK for Python allows you to access Alibaba Cloud services such as Elastic Compute Service (ECS), Server Load Balancer (SLB), ApsaraDB for RDS, and CloudMonitor without the need for complex coding. This topic describes how to obtain and use the Alibaba Cloud SDK for Python.

Online debugging and SDK demo generation

Alibaba Cloud provides OpenAPI Explorer to simplify API usage. You can use OpenAPI Explorer to search for API operations, call API operations, and dynamically generate SDK sample code.

Prerequisites

  • You must have a RAM user and an AccessKey pair (AccessKey ID and AccessKey Secret) to use the Alibaba Cloud SDK for Python. You can go to the Security Management page of the Alibaba Cloud console to create or view the AccessKey pair. Alternatively, you can contact your system administrator to obtain the AccessKey pair.
  • To call API operations of a service, you must first activate the service in the Alibaba Cloud console.

Installation

To install the Alibaba Cloud SDK for Python, perform the following operations:
  1. Install the SDK core library.
    • If you are using Python 2.x, run the following commands to install the SDK core library:
      pip install aliyun-python-sdk-core
    • If you are using Python 3.x, run the following commands to install the SDK core library:
      pip install aliyun-python-sdk-core-v3
  2. Install the SDK of a cloud service.
    The following sample code shows how to install the ECS SDK:
    pip install aliyun-python-sdk-ecs

    You can also download the SDK of a service from GitHub.

Usage

The following sample code shows how to use the Alibaba Cloud SDK for Python:

  1. Create a client object. When you create the client object, you must obtain the Region ID, AccessKey ID, and AccesssKey Secret.
  2. Create an API request and configure the parameters.
  3. Initiate the request and handle the response or exceptions.
    from aliyunsdkcore.client import AcsClient
    from aliyunsdkcore.acs_exception.exceptions import ClientException
    from aliyunsdkcore.acs_exception.exceptions import ServerException
    from aliyunsdkecs.request.v20140526 import DescribeInstancesRequest
    from aliyunsdkecs.request.v20140526 import StopInstanceRequest
    # Create an AcsClient object.
    client = AcsClient(
       "<your-access-key-id>", 
       "<your-access-key-secret>",
       "<your-region-id>"
    );
    # Create a request and set required parameters.
    request = DescribeInstancesRequest.DescribeInstancesRequest()
    request.set_PageSize(10)
    # Initiate the API request and display the returned value.
    response = client.do_action_with_exception(request)
    print response