All Products
Search
Document Center

Simple Message Queue (formerly MNS):Use SMQ SDK for Python to send messages to a topic

Last Updated:Nov 04, 2024

This topic describes the prerequisites and sample code for using Simple Message Queue (SMQ) SDK for Python to send messages to a topic.

Prerequisites

  • SMQ SDK for Python is installed. For more information, see Install SDK for Python.

  • An endpoint and an access credential are configured. For more information, see Configure endpoints and access credentials.

    Important
    • In this example, the endpoint is obtained from the sample.cfg file used for your endpoint configuration. The AccessKey pair and security token are obtained from the environment variables.

    • For more information about how to download the sample code, see the "Sample code" section of the Overview topic.

Select an encoding method for the message body

Specify whether to Base64-encode the message body. For more information, see Select an encoding method for the message body.

Publish messages

  • Run the following command to run the publishmessage.py file to publish multiple messages to a topic:

    python publishmessage.py MyTopic1        

    The following output is returned:

    ==========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
  • Sample code:

    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)
    
    msg_count = 3
    print("%sPublish Message To Topic%s\nTopicName:%s\nMessageCount:%s\n" % (10*"=", 10*"=", topic_name, msg_count))
    
    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)
    
    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)