编写python脚本调用阿里云SDK批量提取阿里云某region(如杭州)全部运行中实例的基本信息并生成表格。
# author lizhejie
import json
import csv
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest
def get_page():
response = client.do_action_with_exception(request)
resj = json.loads(response, encoding='utf-8')
total_count = resj['TotalCount']
total_page, rem = divmod(resj['TotalCount'], 50)
if rem:
total_page = total_page + 1
return total_page
def desinstances(total_page):
count = 1
# 使用字典格式来收集最后实例的信息
total_dict = {}
for page in range(1, total_page+1):
request.set_PageNumber(page)
# 设置set_PageSize(50)中的数字必须大于等于get_page()中divmod方法中第二个参