阿里云SLB Python-sdk

阿里云SLB Python-sdk 范例

1
2
3
4
5
6
7

# 首先安装依赖
pip install aliyun-python-sdk-core
# 如果您使用Python 3.x,执行以下命令,安装阿里云SDK核心库:
# pip install aliyun-python-sdk-core-v3

pip install aliyun-python-sdk-slb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/env python
#coding=utf-8

from aliyunsdkcore import client
from aliyunsdkslb.request.v20140515 import SetBackendServersRequest
import os

clt = client.AcsClient('Access Key ID','Access Key Secret','SLB地区 eg:cn-hangzhou')

ecsA = 'i-bp19cwe60vsu50ntf94b' # ECS后端实例ID
ecsB = 'i-bp13p7zpoi631xenhivo' # ECS后端实例ID
ecsA_weight = '0' # 权重
ecsB_weight = '100'
req_parm1 = "BackendServers"
req_parm2 = [{"ServerId": ecsA, "Weight": ecsA_weight}, {"ServerId": ecsB, "Weight": ecsB_weight}]

# 设置参数
request = SetBackendServersRequest.SetBackendServersRequest()
request.set_accept_format('json')

request.add_query_param('LoadBalancerId', 'lb-bp1akx350m8jwt6snpjxs') # SLB 实例ID
request.add_query_param('RegionId', 'cn-hangzhou')
request.add_query_param(req_parm1, req_parm2)

# 发起请求
response = clt.do_action_with_exception(request)

print (response) # 打印返回