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:
SMQ SDK for Python installed. For more information, see Install SDK for Python
An endpoint and access credentials configured. For more information, see Configure endpoints and access credentials
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:
| Method | Class | Behavior |
|---|---|---|
| Raw | TopicMessage | Sends the message body as-is with no encoding |
| Base64 | Base64TopicMessage | Base64-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 MyTopic1Expected 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****0006The 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)| Parameter | Description |
|---|---|
endpoint | The SMQ service endpoint, loaded from the sample.cfg file |
accid | The AccessKey ID, loaded from environment variables |
acckey | The AccessKey Secret, loaded from environment variables |
token | The security token, loaded from environment variables |
topic_name | The 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 themessage_id.If the topic does not exist, the SDK raises an
MNSExceptionBasewithe.typeset 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 type | Cause | Action |
|---|---|---|
TopicNotExist | The specified topic does not exist | Create the topic first, then retry |
| Other types | Network errors, authentication failures, or other service errors | Check the exception message for details |
What to do next
Learn about message body encoding methods
Download the complete sample code from the "Sample code" section in the Overview topic