01: 騰訊雲API-雲服務器

1.1 雲服務器

  一、騰訊雲SDK使用舉例python

       網址:https://cloud.tencent.com/document/sdk/Pythonjson

#! /usr/bin/env python
# -*- coding: utf-8 -*-
from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
# 導入對應產品模塊的client models。
from tencentcloud.cvm.v20170312 import cvm_client, models

secretId = "*********************************"
secretKey = "*********************************"
try:
    # 實例化一個認證對象,入參須要傳入騰訊雲帳戶secretId,secretKey
    cred = credential.Credential(secretId=secretId, secretKey=secretKey)

    # 實例化要請求產品(以cvm爲例)的client對象
    client = cvm_client.CvmClient(cred, "ap-shanghai")     # 以查詢可用區接口爲例

    # 實例化一個請求對象
    req = models.DescribeZonesRequest()


    # 經過client對象調用想要訪問的接口,須要傳入請求對象
    resp = client.DescribeZones(req)
    # 輸出json格式的字符串回包
    print '##########################'
    print resp.to_json_string()

except TencentCloudSDKException as err:
    print(err)

    
ret = {
    "TotalCount": 5,
    "ZoneSet": [{
        "ZoneState": "UNAVAILABLE",
        "ZoneName": "上海一區",
        "Zone": "ap-shanghai-1",
        "ZoneId": "200001"
    }, {
        "ZoneState": "AVAILABLE",
        "ZoneName": "上海二區",
        "Zone": "ap-shanghai-2",
        "ZoneId": "200002"
    }, {
        "ZoneState": "AVAILABLE",
        "ZoneName": "上海三區",
        "Zone": "ap-shanghai-3",
        "ZoneId": "200003"
    }, {
        "ZoneState": "AVAILABLE",
        "ZoneName": "上海四區",
        "Zone": "ap-shanghai-4",
        "ZoneId": "200004"
    }, {
        "ZoneState": "UNAVAILABLE",
        "ZoneName": "上海五區",
        "Zone": "ap-shanghai-5",
        "ZoneId": "200005"
    }],
    "RequestId": "ebd821c7-ac5c-4dfd-8038-762346bd14d1"
}
騰訊雲SDK使用舉例

  二、地域相關接口api

       網址https://cloud.tencent.com/document/api/213/15708服務器

#! /usr/bin/env python
# -*- coding: utf-8 -*-
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.cvm.v20170312 import cvm_client, models
from day04.settings import secretKey,secretId  # 導入騰訊雲帳戶secretId,secretKey值

# 實例化一個認證對象,入參須要傳入騰訊雲帳戶secretId,secretKey
def instantiates_auth_obj():
    cred = credential.Credential(secretId=secretId, secretKey=secretKey)
    return cred

# 查詢地域列表
def query_zero_list():
    try:
        cred = instantiates_auth_obj()
        httpProfile = HttpProfile()
        httpProfile.endpoint = "cvm.tencentcloudapi.com"
        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        client = cvm_client.CvmClient(cred, "", clientProfile)
        req = models.DescribeRegionsRequest()
        params = '{}'
        req.from_json_string(params)
        resp = client.DescribeRegions(req)
        ret = resp.to_json_string()
        return ret
    except TencentCloudSDKException as err:
        print(err)

print query_zero_list()
d = {
    "TotalCount": 19,
    "RegionSet": [{
        "RegionState": "AVAILABLE",
        "Region": "ap-bangkok",
        "RegionName": "亞太地區(曼谷)"
    }, {
        "RegionState": "AVAILABLE",
        "Region": "ap-beijing",
        "RegionName": "華北地區(北京)"
    },{
        "RegionState": "AVAILABLE",
        "Region": "na-siliconvalley",
        "RegionName": "美國西部(硅谷)"
    }, {
        "RegionState": "AVAILABLE",
        "Region": "na-toronto",
        "RegionName": "北美地區(多倫多)"
    }],
    "RequestId": "8418f014-9ad1-4292-83f1-cf90d27f8ef8"
}
查詢地域列表:如北京、曼谷
#! /usr/bin/env python
# -*- coding: utf-8 -*-
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.cvm.v20170312 import cvm_client, models
from day04.settings import secretKey,secretId  # 導入騰訊雲帳戶secretId,secretKey值

# 實例化一個認證對象,入參須要傳入騰訊雲帳戶secretId,secretKey
def instantiates_auth_obj():
    cred = credential.Credential(secretId=secretId, secretKey=secretKey)
    return cred

# 查詢指定地域可用區列表
def zero_region_list(zones):
    try:
        cred = instantiates_auth_obj()
        httpProfile = HttpProfile()
        httpProfile.endpoint = "cvm.tencentcloudapi.com"
        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        client = cvm_client.CvmClient(cred, zones, clientProfile)
        req = models.DescribeZonesRequest()
        params = '{}'
        req.from_json_string(params)
        resp = client.DescribeZones(req)
        ret = resp.to_json_string()
        return ret

    except TencentCloudSDKException as err:
        print(err)
        return {}

print zero_region_list('ap-beijing')
d = {
    "TotalCount": 5,
    "ZoneSet": [{
        "ZoneState": "AVAILABLE",
        "ZoneName": "北京一區",
        "Zone": "ap-beijing-1",
        "ZoneId": "800001"
    },{
        "ZoneState": "UNAVAILABLE",
        "ZoneName": "北京五區",
        "Zone": "ap-beijing-5",
        "ZoneId": "800005"
    }],
    "RequestId": "011d2390-9dfc-4ba2-a73a-038b740be408"
}
查詢指定地域中有哪些地區:如北京一區、北京二區

  三、實例相關ide

#! /usr/bin/env python
# -*- coding: utf-8 -*-
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.cvm.v20170312 import cvm_client, models
from day04.settings import secretKey,secretId  # 導入騰訊雲帳戶secretId,secretKey值

# 實例化一個認證對象,入參須要傳入騰訊雲帳戶secretId,secretKey
def instantiates_auth_obj():
    cred = credential.Credential(secretId=secretId, secretKey=secretKey)
    return cred

# 查詢指定地域可用區列表(查詢北京地域全部主機信息 InstanceId機器惟一ID)
def zero_hosts_list(zones):
    try:
        cred = instantiates_auth_obj()
        httpProfile = HttpProfile()
        httpProfile.endpoint = "cvm.tencentcloudapi.com"
        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        client = cvm_client.CvmClient(cred, zones, clientProfile)
        req = models.DescribeInstancesRequest()
        params = '{}'
        req.from_json_string(params)
        resp = client.DescribeInstances(req)
        return resp
    except TencentCloudSDKException as err:
        print(err)
        return {}


print zero_hosts_list('ap-beijing')
d = {
    "InstanceSet": [{
        "RenewFlag": "NOTIFY_AND_AUTO_RENEW",
        "InstanceState": "RUNNING",
        "LoginSettings": {
            "KeyIds": None,
            "Password": None,
            "KeepImageLogin": None
        },
        "RestrictState": "NORMAL",
        "ExpiredTime": "2019-04-11T07:25:00Z",
        "Memory": 1,
        "CreatedTime": "2019-03-11T07:25:00Z",
        "CPU": 1,
        "PublicIpAddresses": ["118.89.245.148"],
        "Tags": [{
            "Key": "costcenter",
            "Value": "devops"
        }, {
            "Key": "負責人",
            "Value": "zihe.feng"
        }],
        "InstanceId": "ins-3n9jqe9x",
        "ImageId": "img-qp03e2j7",
        "StopChargingMode": "NOT_APPLICABLE",
        "InstanceChargeType": "PREPAID",
        "InstanceType": "S2.SMALL1",
        "SystemDisk": {
            "DiskSize": 100,
            "DiskId": "disk-bxp7e5c1",
            "DiskType": "CLOUD_SSD"
        },
        "Placement": {
            "ProjectId": 0,
            "HostIds": None,
            "Zone": "ap-beijing-1"
        },
        "PrivateIpAddresses": ["172.20.0.10"],
        "OsName": "CentOS 7.3 64位",
        "SecurityGroupIds": ["sg-od0hrpvh"],
        "InstanceName": "bj-test-common-ss-1",
        "DataDisks": None,
        "VirtualPrivateCloud": {
            "SubnetId": "subnet-qebil5dp",
            "AsVpcGateway": False,
            "VpcId": "vpc-fbv2dybq",
            "PrivateIpAddresses": None
        },
        "InternetAccessible": {
            "PublicIpAssigned": None,
            "InternetChargeType": "TRAFFIC_POSTPAID_BY_HOUR",
            "BandwidthPackageId": None,
            "InternetMaxBandwidthOut": 200
        }
    }, {
        "RenewFlag": "NOTIFY_AND_AUTO_RENEW",
        "InstanceState": "RUNNING",
        "LoginSettings": {
            "KeyIds": ["skey-h64mpzpz"],
            "Password": None,
            "KeepImageLogin": None
        },
        "RestrictState": "NORMAL",
        "ExpiredTime": "2019-04-07T11:11:42Z",
        "Memory": 56,
        "CreatedTime": "2019-03-07T11:11:42Z",
        "CPU": 28,
        "PublicIpAddresses": ["154.8.155.101"],
        "Tags": [],
        "InstanceId": "ins-33hru7oj",
        "ImageId": "img-qp03e2j7",
        "StopChargingMode": "NOT_APPLICABLE",
        "InstanceChargeType": "PREPAID",
        "InstanceType": "GN2.7XLARGE56",
        "SystemDisk": {
            "DiskSize": 50,
            "DiskId": "ldisk-czzwc94e",
            "DiskType": "LOCAL_SSD"
        },
        "Placement": {
            "ProjectId": 0,
            "HostIds": None,
            "Zone": "ap-beijing-2"
        },
        "PrivateIpAddresses": ["172.21.0.15"],
        "OsName": "CentOS 7.3 64位",
        "SecurityGroupIds": ["sg-od0hrpvh"],
        "InstanceName": "bj-prod-research-gpu-2",
        "DataDisks": [{
            "DeleteWithInstance": None,
            "DiskSize": 1650,
            "DiskId": "ldisk-1xfm3yi6",
            "DiskType": "LOCAL_SSD"
        }],
        "VirtualPrivateCloud": {
            "SubnetId": "subnet-iyhhdfun",
            "AsVpcGateway": False,
            "VpcId": "vpc-pmccgax8",
            "PrivateIpAddresses": None
        },
        "InternetAccessible": {
            "PublicIpAssigned": None,
            "InternetChargeType": "TRAFFIC_POSTPAID_BY_HOUR",
            "BandwidthPackageId": None,
            "InternetMaxBandwidthOut": 200
        }
    }],
    "TotalCount": 11,
    "RequestId": "b68ed268-d528-48e8-aa29-416b126d7489"
}
查詢實例機型列表:如查詢北京這個地域全部主機列表

 

 

 

 

 

 

 

 

11111111111111111111spa

相關文章
相關標籤/搜索