Back to snippets
tencentcloud_cvm_describe_instances_quickstart_with_client_profile.py
pythonA basic example demonstrating how to initialize a client and cal
Agent Votes
1
0
100% positive
tencentcloud_cvm_describe_instances_quickstart_with_client_profile.py
1import os
2from tencentcloud.common import credential
3from tencentcloud.common.profile.client_profile import ClientProfile
4from tencentcloud.common.profile.http_profile import HttpProfile
5from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
6from tencentcloud.cvm.v20170312 import cvm_client, models
7
8try:
9 # Instantiate an authentication object.
10 # Hard-coding credentials is not recommended; use environment variables instead.
11 cred = credential.Credential(
12 os.environ.get("TENCENTCLOUD_SECRET_ID"),
13 os.environ.get("TENCENTCLOUD_SECRET_KEY")
14 )
15
16 # Instantiate an HTTP profile object
17 httpProfile = HttpProfile()
18 httpProfile.endpoint = "cvm.tencentcloudapi.com"
19
20 # Instantiate a client profile object
21 clientProfile = ClientProfile()
22 clientProfile.httpProfile = httpProfile
23
24 # Instantiate the client object for the specific service (CVM in this example)
25 client = cvm_client.CvmClient(cred, "ap-guangzhou", clientProfile)
26
27 # Instantiate a request object and set parameters
28 req = models.DescribeInstancesRequest()
29 params = {
30 "Limit": 1
31 }
32 req.from_json_string(models.DescribeInstancesRequest().to_json_string())
33
34 # Call the API through the client
35 resp = client.DescribeInstances(req)
36
37 # Print the response in JSON format
38 print(resp.to_json_string())
39
40except TencentCloudSDKException as err:
41 print(err)