Overview
Welcome to Tencent Cloud Software Development Kit (SDK) 3.0, a companion tool for the TencentCloud API 3.0 platform. SDK 3.0 is unified and features the same SDK usage, API call methods, error codes, and returned packet formats for different programming languages.
This document describes how to use, debug, and connect to TencentCloud APIs with the SDK for Python 3.0 as an example.
This version currently supports various Tencent Cloud products such as CVM, VPC, and CBS and will support more products in the future.
Dependent Environment
Python 2.7 and 3.6–3.9.
Get the security credential, which consists of SecretId
and SecretKey
. SecretId
is used to identify the API requester, while SecretKey
is a key used for signature string encryption and authentication by the server. You can get them on the API Key Management page as shown below:
Note:
Your security credential represents your account identity and granted permissions, which is equivalent to your login password. Do not disclose it to others.
Get the calling address (endpoint), which is generally in the format of *.tencentcloudapi.com
and varies by product. For example, the endpoint of CVM is cvm.tencentcloudapi.com
. For specific endpoints, please see the API documentation of the corresponding product . Installing SDK
Method 1. Install through pip (recommended)
You can install the Tencent Cloud SDK for Python into your project through pip. If you haven't installed pip in your project environment yet, install it first as instructed in Installation.
Run the following command on the command line to install the SDK for Python. pip install --upgrade tencentcloud-sdk-python
Note:
If you have both Python 2 and Python 3 environments, you need to use the pip3 command to install.
Users in the Chinese mainland can use a Tencent Cloud mirror source to speed up the download by running pip install -i https://mirrors.tencent.com/pypi/simple/ --upgrade tencentcloud-sdk-python
for example.
Note:
If you only want to use the package of a specific product, such as CVM, you can install it separately, but this method cannot work together with the full installation method. For example, run pip install --upgrade tencentcloud-sdk-python-common tencentcloud-sdk-python-cvm
.
Method 2. Install through source package
$ cd tencentcloud-sdk-python
$ python setup.py install
Using SDK
The following takes the instance list querying API as an example.
from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.cvm.v20170312 import cvm_client, models
try:
cred = credential.Credential("secretId", "secretKey")
client = cvm_client.CvmClient(cred, "ap-shanghai")
req = models.DescribeInstancesRequest()
resp = client.DescribeInstances(req)
print(resp.to_json_string())
except TencentCloudSDKException as err:
print(err)
import sys
import logging
from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.cvm.v20170312 import cvm_client, models
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
try:
cred = credential.Credential("SecretId", "SecretKey")
httpProfile = HttpProfile()
httpProfile.protocol = "https"
httpProfile.keepAlive = True
httpProfile.reqMethod = "GET"
httpProfile.reqTimeout = 30
httpProfile.endpoint = "cvm.ap-shanghai.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.signMethod = "TC3-HMAC-SHA256"
clientProfile.language = "en-US"
clientProfile.httpProfile = httpProfile
client = cvm_client.CvmClient(cred, "ap-shanghai", clientProfile)
req = models.DescribeInstancesRequest()
respFilter = models.Filter()
respFilter.Name = "zone"
respFilter.Values = ["ap-shanghai-1", "ap-shanghai-2"]
req.Filters = [respFilter]
resp = client.DescribeInstances(req)
print(resp.to_json_string(indent=2))
print(resp.TotalCount)
except TencentCloudSDKException as err:
print(err)
Common Client Call Method
Starting from v3.0.396
, Tencent Cloud SDK for Python supports the use of Common Client
mode for requests. You only need to install the tencentcloud-sdk-python-common
package to initiate calls to any Tencent Cloud product.
Note:
You must clearly know the parameters required by the called API; otherwise, the call may fail.
For more information on Common Client
, please see example. More Examples
You can find more detailed samples in the examples
directory in the GitHub repository. Relevant Configuration
Proxy
If there is a proxy in your environment, you can set the proxy in the following two ways:
Specify the proxy when initializing HttpProfile. For more information, please see the example. You need to set the system environment variable https_proxy
.
Otherwise, it may not be called normally, and a connection timeout exception will be thrown.
FAQs
Certificate issue
When you install Python 3.6 or above on macOS, you may encounter a certificate error:
Error: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1056).
This is because that on macOS, Python no longer uses the system's default certificate and does not provide a certificate itself. When an HTTPS request is made, the certificate provided by the certifi
library needs to be used, but the SDK does not support specifying it; therefore, you can only solve this problem by installing the certificate with the sudo "/Applications/Python 3.6/Install Certificates.command"
command.
Although this problem should not occur in Python 2, there may be similar situations in specific user environments, which can also be solved with sudo /Applications/Python 2.7/Install Certificates.command
.
Was this page helpful?