有时候想知道腾讯云或者阿里云账号上还剩下多少余额,又不想登录上去看,想了一下写个python脚本挺方便的,分享下
安装模块 pip install --upgrade tencentcloud-sdk-python pip install --upgrade aliyun-python-sdk-core pip install aliyun-python-sdk-bssopenapid
代码:
import json import sys import QueryAccountBalanceRequest from tencentcloud.common import credential from tencentcloud.common.profile.client_profile import ClientProfile from tencentcloud.common.profile.http_profile import HttpProfile from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException from tencentcloud.billing.v20180709 import billing_client, models from aliyunsdkcore.client import AcsClient from aliyunsdkcore.acs_exception.exceptions import ClientException from aliyunsdkcore.acs_exception.exceptions import ServerException from aliyunsdkbssopenapi.request.v20171214.QueryAccountBalanceRequest tencentcloud_KeyId="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" #腾讯云生成的APIid tencentcloud_KeySecret="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" #腾讯云生成的APIkey aliyuncloud_KeyId='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' #阿里云生成的APIid aliyuncloud_KeySecret='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' #阿里云生成的APIkey def GetAccout(ak,aks): try: cred = credential.Credential(ak, aks) httpProfile = HttpProfile() httpProfile.endpoint = "billing.tencentcloudapi.com" clientProfile = ClientProfile() clientProfile.httpProfile = httpProfile client = billing_client.BillingClient(cred, "", clientProfile) req = models.DescribeAccountBalanceRequest() params = { } req.from_json_string(json.dumps(params)) resp = client.DescribeAccountBalance(req) return resp.__dict__ except TencentCloudSDKException as err: print(err) def AvailableAmount(Akid,AkSecret): client = AcsClient(Akid, AkSecret, 'cn-hangzhou') request = QueryAccountBalanceRequest() request.set_accept_format('json') response = client.do_action_with_exception(request) return (json.loads(response)) if __name__ == "__main__": if len(sys.argv) == 2: if sys.argv[1] == 'tencentcloud': Mon =('%.2f' %(float(GetAccout(tencentcloud_KeyId,tencentcloud_KeySecret)['Balance'])/float(100))) print(Mon) elif sys.argv[1] == 'aliyuncloud': Mon = AvailableAmount(aliyuncloud_KeyId,aliyuncloud_KeySecret)['Data']['AvailableAmount'].replace(',','') print(Mon) else: print('python view_balance.py tencentcloud | aliyuncloud')
测试:
Python view_balance.py tencentcloud Python view_balance.py aliyuncloud