全部產品
Search
文件中心

Alibaba Cloud SDK:代理配置

更新時間:Jul 01, 2024

本文為您介紹V2.0 Python SDK對於網路代理程式的配置方式。

V2.0 SDK代理配置方式

  • 支援通過運行時參數(RuntimeOption)配置當前請求的代理。

    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)
    
  • 在初始化用戶端階段,支援通過Config對請求的代理進行設定。

    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)
    
  • 支援通過環境變數配置代理:

    • HTTP_PROXY 或者 http_proxy

    • HTTPS_PROXY或者 https_proxy

說明

代理配置優先順序為:RuntimeOption->Client->環境變數,優先順序依次降低。