代理配置

更新时间:

本文为您介绍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->环境变量,优先级依次降低。