IoT Platform provides developers with an SDK for Python. This article describes how to install and configure IoT Platform SDK for Python. This article also describes how to use the SDK to call the API operations of IoT Platform.
Install the SDK
Install a Python integrated development environment (IDE).
Download a Python installation package from the official Python website and install Python. Python 2.7.x and 3.x are supported.
Install pip that is used to manage Python packages.If you have installed pip, skip this step.
Download a pip installation package from the official pip website and install pip.
Run the following commands as an administrator to install IoT Platform SDK for Python.
For more information, see IoT Platform SDK for Python.
sudo pip install aliyun-python-sdk-core sudo pip install aliyun-python-sdk-iot
Import the files that are related to IoT Platform SDK for Python to a Python file.
from aliyunsdkcore import client from aliyunsdkiot.request.v20180120 import RegisterDeviceRequest from aliyunsdkiot.request.v20180120 import PubRequest ...
Initialize the SDK
The following example shows how to initialize the SDK when IoT Platform is activated in the China (Shanghai) region:
Use the client.AcsClient(accessKeyId, accessKeySecret, 'cn-shanghai')
method to load the SDK initialization information.
accessKeyId = '<your accessKey>'
accessKeySecret = '<your accessSecret>'
clt = client.AcsClient(accessKeyId, accessKeySecret, 'cn-shanghai')
Parameter | Description |
accessKeyId | The AccessKey ID of your Alibaba Cloud account. You can go to the Alibaba Cloud Management Console to create or view your AccessKey ID. |
accessKeySecret | The AccessKey secret of you Alibaba Cloud account. |
clt | The initialized SDK client. You can view the region in the upper-left corner of the IoT Platform console. For more information about region IDs, see Supported regions. |
Initiate a request
The SDK encapsulates a class for each API operation. The class name is in the ${Operation name}+"Request"
format. You can use the request
instance of this class and call the "set_"+${request parameters}
method to specify the request parameters. You can use the clt client and call the do_action_with_exception(request)
method to obtain a response.
For more information about the API operations of IoT Platform, see List of operations by function. For more information about the request
and response
parameters of each API operation, see the API documentation.
The following example shows how to call the Pub operation to publish a message to a topic. For more information about request parameters, see Pub.
In the following sample code, ${iotInstanceId}
specifies the ID of an instance. You can view the ID of the instance on the Overview page in the IoT Platform console.
If your instance has an ID, you must specify the ID for this parameter. Otherwise, the call fails.
If no Overview page or ID is generated for your instance, you do not need to configure this parameter. You must delete the request code that is related to the IotInstanceId parameter or specify an empty string (
""
) for the parameter. Otherwise, the call fails.
For more information about IoT Platform instances, see Overview. For information about how to purchase an instance, see Purchase Enterprise Edition instances. For more information, see FAQ about IoT Platform instances.
request = PubRequest.PubRequest()
request.set_accept_format('json') # The format in which the response is returned. By default, the XML format is used. In this example, the JSON format is used.
request.set_IotInstanceId('iotInstanceId')
request.set_ProductKey('productKey')
request.set_TopicFullName('/productKey/deviceName/user/get') # The full name of the topic to which the message is sent.
request.set_MessageContent('aGVsbG8gd29ybGQ=') #hello world Base64 String
request.set_Qos(0)
result = clt.do_action_with_exception(request)
print('result : ' + result)
Appendix: Sample code
You can view or download the sample code of API operations in IoT Platform SDK Sample Center. Sample code of SDKs for Java, Python, PHP, .NET, and Go is provided.
Alibaba Cloud OpenAPI Explorer provides online debugging tools for API operations. On the API Debugging page, you can search for API operations, call API operations, and generate sample code for API operations of different SDKs. On the right side of the page, you can view the sample code of an SDK on the Sample Code tab. On the Debugging Result tab, you can view the actual request URL and response in the JSON format.