• Python查看云平台余额


    有时候想知道腾讯云或者阿里云账号上还剩下多少余额,又不想登录上去看,想了一下写个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
    

      

     

  • 相关阅读:
    Mac安装WineHQ
    Ubuntu 16.04下使用Wine安装文件比对工具Beyond Compare 4
    Ubuntu 16.04下安装WineHQ
    Ubuntu 16.04下使用Wine安装PowerDesigner15
    Ubuntu 16.04下使用Wine安装Windows版的微信(不太完美)
    Ubuntu 16.04下使用Wine安装Xshell 4和Xftp 4
    MySQL中数据类型(char(n)、varchar(n)、nchar(n)、nvarchar(n)的区别)(转)
    Ubuntu 16.04安装MongoDB的GUI工具RoboMongo
    Linux下Shell的快捷键(转)
    Jackson反序列化错误:com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field的解决方法
  • 原文地址:https://www.cnblogs.com/lucktomato/p/16109602.html
Copyright © 2020-2023  润新知