This topic describes how to use the Python SDK to access Alibaba Cloud IoT Platform and receive messages subscribed.

Development environment

In this example, Python 2.7 is used.

Download the SDK

We recommend that you use Apache Qpid Proton 0.29.0 for the Python AMQP SDK. Python APIs are encapsulated in the library. You can visit Qpid Proton 0.29.0 to download the library and view its instructions.

Install Proton. For information about the installation instructions, see Installing Qpid Proton.

After you complete the installation, run the following Python command to check whether the SSL library is successfully installed:

import proton;print('%s' % 'SSL present' if proton.SSL.present() else 'SSL NOT AVAILABLE')

Sample code

For information about parameters in the following demo, see AMQP client access instructions.

# encoding=utf-8
import sys
import logging
import time
from proton.handlers import MessagingHandler
from proton.reactor import Container
import hashlib
import hmac
import base64

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
console_handler = logging.StreamHandler(sys.stdout)

def current_time_millis():
    return str(int(round(time.time() * 1000)))

def do_sign(secret, sign_content):
    m =, sign_content, digestmod=hashlib.sha1)
    return base64.b64encode(m.digest())

class AmqpClient(MessagingHandler):
    def __init__(self):
        super(AmqpClient, self).__init__()

    def on_start(self, event):
        url = "amqps://${uid}.iot-amqp.${regionId}"
        accessKey = "${YourAccessKeyID}"
        accessSecret = "${YourAccessKeySecret}"
        consumerGroupId = "${YourConsumerGroupId}"
        clientId = "${YourClientId}"
        signMethod = "hmacsha1"
        timestamp = current_time_millis()
        userName = clientId + "|authMode=aksign" + ",signMethod=" + signMethod \
                        + ",timestamp=" + timestamp + ",authId=" + accessKey \
                        + ",consumerGroupId=" + consumerGroupId + "|"
        signContent = "authId=" + accessKey + "&timestamp=" + timestamp
        passWord = do_sign(accessSecret.encode("utf-8"), signContent.encode("utf-8"))
        conn = event.container.connect(url, user=userName, password=passWord, heartbeat=60)
        self.receiver = event.container.create_receiver(conn)

    def on_connection_opened(self, event):"Connection established, remoteUrl: %s", event.connection.hostname)

    def on_connection_closed(self, event):"Connection closed: %s", self)

    def on_connection_error(self, event):"Connection error")

    def on_transport_error(self, event):
        if event.transport.condition:
                logger.error("%s: %s: %s" % (
          , event.transport.condition.description,
                logger.error("%s: %s" % (, event.transport.condition.description))
            logging.error("Unspecified transport error")

    def on_message(self, event):
        message = event.message
        content = message.body.decode('utf-8')
        topic ="topic")
        message_id ="messageId")
        print("receive message: message_id=%s, topic=%s, content=%s" % (message_id, topic, content))