Application Python SDK
IoTDA provides an application SDK in Python for developers. This section describes how to install and configure the Python SDK and how to use it to call application-side APIs.
Obtaining and Installing the SDK
- Install the Python development environment.
Visit the Python website, and download and install the Python development environment.
The Python SDK can be used in Python 3 and later versions.
- Install PiP.
Visit the PiP website, and download and install PiP.
- Install the Python SDK.
Run the following commands to install the Python SDK core library and related service libraries.
# Install the core library. pip install huaweicloudsdkcore # Install the IoTDA service library. pip install huaweicloudsdkiotda
Code Sample
The following code sample shows how to use the Python SDK to call API Querying the Device List.
- Create a credential.
- Create and initialize an IoTDAClient instance.
- Instantiate a request object.
- Call the API for querying the device list.
from huaweicloudsdkcore.exceptions import exceptions from huaweicloudsdkcore.region.region import Region from huaweicloudsdkiotda.v5 import * from huaweicloudsdkcore.auth.credentials import BasicCredentials from huaweicloudsdkcore.auth.credentials import DerivedCredentials if __name__ == "__main__": # There will be security risks if the AK/SK used for authentication is directly written into code. Encrypt the AK/SK in the configuration file or environment variables for storage; # In this example, the AK/SK stored in the environment variables are used. Configure the environment variables HUAWEICLOUD_SDK_AK and HUAWEICLOUD_SDK_SK in the local environment first. ak = os.environ["HUAWEICLOUD_SDK_AK"] sk = os.environ["HUAWEICLOUD_SDK_SK"] project_id = "<YOUR PROJECTID>" # region_id: If CN East-Shanghai1 is used, enter cn-east-3. If CN North-Beijing4 is used, enter cn-north-4. If CN South-Guangzhou is used, enter cn-south-1. region_id = "<YOUR REGION ID>" # endpoint: On the console, choose Overview and click Access Addresses to view the HTTPS application access address. endpoint = "<YOUR ENDPOINT>" # For the standard or enterprise edition, create a region object. REGION = Region(region_id, endpoint) # Create a credential. # Create and initialize a BasicCredentials instance. credentials = BasicCredentials(ak, sk, project_id) # with_derived_predicate is used for the standard or enterprise edition. For the basic edition, delete the line. credentials.with_derived_predicate(DerivedCredentials.get_default_derived_predicate()) # For the basic edition, select the region object in IoTDAClient. For example, .with_region(IoTDARegion.CN_NORTH_4). # For the standard or enterprise edition, use the region object created by yourself. client = IoTDAClient.new_builder() \ .with_credentials(credentials) \ .with_region(REGION) \ .build() try: # Instantiate a request object. request = ListDevicesRequest() # Call the API for querying the device list. response = client.list_devices(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
Parameter
Description
ak
Access key ID of your Huawei Cloud account. You can create and view an AK/SK on the Access Keys.
page of the Huawei Cloud console. For details, seesk
Secret access key (SK) of your Huawei Cloud account.
project_id
Project ID. For details on how to obtain a project ID, see Obtaining a Project ID.
IoTDARegion.CN_NORTH_4
Region where the IoT platform to be accessed is located. The available regions of the IoT platform have been defined in the SDK code iotda_region.py.
On the console, you can view the region name of the current service and the mapping between regions and endpoints. For details, see Platform Connection Information.
region_id
If CN East-Shanghai1 is used, enter cn-east-3. If CN North-Beijing4 is used, enter cn-north-4. If CN South-Guangzhou is used, enter cn-south-4.
endpoint
On the console, choose Overview and click Access Addresses to view the HTTPS application access address.
Additional Information
For details on the project source code and usage guide, see Huawei Cloud Python Software Development Kit (Python SDK).
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot