All Products
Search
Document Center

Simple Message Queue (formerly MNS):Publish messages to a topic by using SMQ SDK for Python

Last Updated:Mar 11, 2026

Publish raw and Base64-encoded messages to a Simple Message Queue (formerly MNS) topic by using SMQ SDK for Python.

Prerequisites

Before you begin, make sure that you have:

Important

The endpoint in this example is loaded from the sample.cfg configuration file. The AccessKey pair and security token are loaded from environment variables. To download the complete sample code, see the "Sample code" section in the Overview topic.

Message body encoding methods

SMQ supports two message body encoding methods:

MethodClassBehavior
RawTopicMessageSends the message body as-is with no encoding
Base64Base64TopicMessageBase64-encodes the message body before sending

For guidance on choosing an encoding method, see Select an encoding method for the message body.

Publish messages

Run the publish_message.py file to publish messages to a topic:

python publish_message.py MyTopic1

Expected output:

==========Publish Message To Topic==========
TopicName:MyTopic1
MessageCount:3

Publish Raw Message Succeed. MessageBody:I am test message 0. MessageID:4EA4CF419C3A7FC67FA95077****0001
Publish Raw Message Succeed. MessageBody:I am test message 1. MessageID:4EA4CF419C3A7FC67FA95077****0002
Publish Raw Message Succeed. MessageBody:I am test message 2. MessageID:4EA4CF419C3A7FC67FA95077****0003
Publish Base64 Encoded Message Succeed. MessageBody:I am test message 0. MessageID:4EA4CF419C3A7FC67FA95077****0004
Publish Base64 Encoded Message Succeed. MessageBody:I am test message 1. MessageID:4EA4CF419C3A7FC67FA95077****0005
Publish Base64 Encoded Message Succeed. MessageBody:I am test message 2. MessageID:4EA4CF419C3A7FC67FA95077****0006

The script publishes three raw messages and three Base64-encoded messages to MyTopic1, for a total of six messages.

Code walkthrough

The core code in publish_message.py performs three steps: initialize the account, publish raw messages, and publish Base64-encoded messages.

Step 1: Initialize the account and get the topic

Create an Account object with your endpoint and credentials, then get a reference to the target topic:

my_account = Account(endpoint, accid, acckey, token)
topic_name = sys.argv[1] if len(sys.argv) > 1 else "MySampleTopic"
my_topic = my_account.get_topic(topic_name)
ParameterDescription
endpointThe SMQ service endpoint, loaded from the sample.cfg file
accidThe AccessKey ID, loaded from environment variables
acckeyThe AccessKey Secret, loaded from environment variables
tokenThe security token, loaded from environment variables
topic_nameThe name of the target topic. Defaults to MySampleTopic if no command-line argument is provided

Step 2: Publish raw messages

Use TopicMessage to publish messages without encoding:

msg_count = 3

for i in range(msg_count):
    try:
        msg_body = u"I am test message %s." % i
        msg = TopicMessage(msg_body)
        re_msg = my_topic.publish_message(msg)
        print("Publish Raw Message Succeed. MessageBody:%s MessageID:%s" % (msg_body, re_msg.message_id))
    except MNSExceptionBase as e:
        if e.type == "TopicNotExist":
            print("Topic not exist, please create it.")
            sys.exit(1)
        print("Publish Raw Message Fail. Exception:%s" % e)
  • TopicMessage(msg_body) wraps the message body as a raw (unencoded) message.

  • my_topic.publish_message(msg) publishes the message to the topic and returns a response that contains the message_id.

  • If the topic does not exist, the SDK raises an MNSExceptionBase with e.type set to "TopicNotExist".

Step 3: Publish Base64-encoded messages

Use Base64TopicMessage to Base64-encode the message body before publishing:

for i in range(msg_count):
    try:
        msg_body = u"I am test message %s." % i
        msg = Base64TopicMessage(msg_body)
        re_msg = my_topic.publish_message(msg)
        print("Publish Base64 Encoded Message Succeed. MessageBody:%s MessageID:%s" % (msg_body, re_msg.message_id))
    except MNSExceptionBase as e:
        if e.type == "TopicNotExist":
            print("Topic not exist, please create it.")
            sys.exit(1)
        print("Publish Base64 Encoded Message Fail. Exception:%s" % e)

The only difference from Step 2 is the message class: Base64TopicMessage automatically Base64-encodes the message body before publishing.

Error handling

The code catches MNSExceptionBase and checks the e.type attribute to identify the error:

Error typeCauseAction
TopicNotExistThe specified topic does not existCreate the topic first, then retry
Other typesNetwork errors, authentication failures, or other service errorsCheck the exception message for details

What to do next