1. 程式人生 > >Python呼叫aliyun API設定阿里雲負載均衡虛擬伺服器組權重

Python呼叫aliyun API設定阿里雲負載均衡虛擬伺服器組權重

尊重他人努力的結果,該文件稍稍改了些

 轉載:http://blog.51cto.com/chinahao/2054307

CentOS Linux release 7.2.1511 (Core)

[[email protected] ~]# cat /proc/version 
Linux version 3.10.0-327.el7.x86_64 ([email protected]) (gcc version 4.8.3 20140911 (Red Hat 4.8.3-9) (GCC) ) #1 SMP Thu Nov 19 22:10:57 UTC 2015
[[email protected]
~]# [[email protected] ~]# uname -a Linux localhost.localdomain 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux [[email protected] ~]# [[email protected] ~]# python -V Python 2.7.5
[[email protected] ~]#  yum groupinstall "Development Tools"
[
[email protected]
~]# yum install python-setuptools ntsysv python-devel setuptool [[email protected] ~]# pip install setuptools [[email protected] ~]# pip install aliyun-python-sdk-core [[email protected] ~]# pip install aliyun-python-sdk-slb


 

#!/usr/bin/env python
#coding=utf-8

import sys
import json
from aliyunsdkcore import client
from aliyunsdkslb.request.v20140515 import SetVServerGroupAttributeRequest
from aliyunsdkslb.request.v20140515 import DescribeVServerGroupAttributeRequest

clt = client.AcsClient('阿里ak','阿里sk','區域id')

# 查詢
## 設定引數
request = DescribeVServerGroupAttributeRequest.DescribeVServerGroupAttributeRequest()
request.set_accept_format('json')


request.add_query_param('RegionId', '區域id')
request.add_query_param('VServerGroupId', '虛擬伺服器分組id')

## 發起請求
response = clt.do_action_with_exception(request)

## 輸出結果
print("查詢結果:", response, '\n')
#
# # 處理
weight = json.loads(response)["BackendServers"]["BackendServer"]

for i in range(len(weight)):
    weight[i]["Weight"] = sys.argv[i+1]
    print("引數設定Client"+str(i+1)+':', weight[i]["Weight"], '\n')

weight = json.dumps(weight)

# 設定
## 設定引數
request = SetVServerGroupAttributeRequest.SetVServerGroupAttributeRequest()
request.set_accept_format('json')

request.add_query_param('RegionId', '區域id')
request.add_query_param('BackendServers', weight)
request.add_query_param('VServerGroupId', '虛擬伺服器分組id')

## 發起請求
response = clt.do_action_with_exception(request)

## 輸出結果
print("設定結果:", response, '\n')

以上指令碼只修改有中文的

下面是驗證結果