All Products
Search
Document Center

Alibaba Cloud SDK:Configure a proxy

Last Updated:Jul 01, 2024

This topic describes how to configure a proxy in SDK for Python V2.0.

Methods

  • Configure a proxy by using RuntimeOptions in SDK for Python V2.0.

    import os
    
    from alibabacloud_ecs20140526.client import Client as EcsClient
    from alibabacloud_ecs20140526.models import DescribeRegionsRequest
    from alibabacloud_tea_openapi.models import Config
    from alibabacloud_tea_util.models import RuntimeOptions
    
    config = Config(
        access_key_id=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID'),
        access_key_secret=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET'),
        endpoint='ecs-cn-hangzhou.aliyuncs.com'
    )
    ecs_client = EcsClient(config)
    
    runtimeOptions = RuntimeOptions(
        http_proxy='http://127.0.0.1:9898',
        https_proxy='http://user:password@127.0.0.1:8989'
    )
    request = DescribeRegionsRequest()
    response = ecs_client.describe_regions_with_options(request, runtimeOptions)
    print(response.body)
    
  • Configure a proxy by using a Config object when you initialize an SDK client.

    import os
    
    from alibabacloud_ecs20140526.client import Client as EcsClient
    from alibabacloud_ecs20140526.models import DescribeRegionsRequest
    from alibabacloud_tea_openapi.models import Config
    from alibabacloud_tea_util.models import RuntimeOptions
    
    config = Config(
        access_key_id=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID'),
        access_key_secret=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET'),
        endpoint='ecs-cn-hangzhou.aliyuncs.com',
        http_proxy='http://127.0.0.1:9898',
        https_proxy='http://user:password@127.0.0.1:8989'
    )
    ecs_client = EcsClient(config)
    
    runtimeOptions = RuntimeOptions()
    request = DescribeRegionsRequest()
    response = ecs_client.describe_regions_with_options(request, runtimeOptions)
    print(response.body)
    
  • Configure a proxy by using one of the following environment variables:

    • HTTP_PROXY or http_proxy

    • HTTPS_PROXY or https_proxy

Note

The priority levels of the methods that are used to configure a proxy are listed in descending order: use RuntimeOptions, use a Config object when you initialize an SDK client, and use an environment variable.