This topic describes how to use Message Service (MNS) SDK for Python to create a queue, send, receive and delete messages, and delete the queue.
Step 1: Prepare the environment
Download the latest version of MNS SDK for Python, decompress the package, and then go to the mns_python_sdk subdirectory.
Open the sample.cfg file, and specify an AccessKey ID, an AccessKey secret, an endpoint, and a Security Token Service (STS) token.
AccessKey ID and AccessKey secret
The AccessKey pair that is used to call API operations in Alibaba Cloud.
If you use an Alibaba Cloud account, go to the AccessKey Pair page of the Alibaba Cloud Management Console to create and view your AccessKey pair.
If you use MNS as a Resource Access Management (RAM) user, log on to the RAM console to view the AccessKey pair.
Endpoint
The endpoint that is used to access MNS. To view the endpoints, log on to the MNS console. For more information, see the "Obtain the endpoints of a queue" section of the Manage queues in the console topic.
The endpoints of MNS vary based on the region.
STS token
The temporary access credential that is provided by RAM. If you access Alibaba Cloud services as a RAM user or by using an Alibaba Cloud account, you do not need to specify an STS token. For more information, see What is STS?
Go to the sample directory in which all the sample code resides.
Step 2: Create a queue
Run the createqueue.py file to create a queue. The default name of the new queue is MySampleQueue. You can also specify another queue name. For more information, see Queue.
Run the following command:
python createqueue.py MyQueue1
The following result is returned:
Create Queue Succeed! QueueName:MyQueue1
Sample code:
The endpoint, AccessKey ID, AccessKey secret, and STS token are retrieved from the sample.cfg file that is specified in Step 1.
my_account = Account(endpoint, accid, acckey, token) queue_name = sys.argv[1] if len(sys.argv) > 1 else "MySampleQueue" my_queue = my_account.get_queue(queue_name) queue_meta = QueueMeta() try: queue_url = my_queue.create(queue_meta) print "Create Queue Succeed! QueueName:%s\n" % queue_name except MNSExceptionBase, e: if e.type == "QueueAlreadyExist": print "Queue already exist, please delete it before creating or use it directly." sys.exit(0) print "Create Queue Fail! Exception:%s\n" % e
Step 3: Send messages
Run the sendmessage.py file to send multiple messages to the queue. You must specify the name of the queue that is created in Step 2 by using a specific parameter. For more information, see QueueMessage.
Run the following command:
python sendmessage.py MyQueue1
The following result is returned:
==========Send Message To Queue========== QueueName:MyQueue1 MessageCount:3 Send Message Succeed! MessageBody:I am test message 0. MessageID:3EBE662B52BC99BC-1-154BD99CCA7-200000001 Send Message Succeed! MessageBody:I am test message 1. MessageID:64B92941FC57837F-2-154BD99CCCE-200000001 Send Message Succeed! MessageBody:I am test message 2. MessageID:3EBE662B52BC99BC-1-154BD99CCF0-200000002
Sample code:
The endpoint, AccessKey ID, AccessKey secret, and STS token are retrieved from the sample.cfg file that is specified in Step 1.
my_account = Account(endpoint, accid, acckey, token) queue_name = sys.argv[1] if len(sys.argv) > 1 else "MySampleQueue" my_queue = my_account.get_queue(queue_name) msg_count = 3 print "%sSend Message To Queue%s\nQueueName:%s\nMessageCount:%s\n" % (10*"=", 10*"=", queue_name, msg_count) for i in range(msg_count): try: msg_body = "I am test message %s." % i msg = Message(msg_body) re_msg = my_queue.send_message(msg) print "Send Message Succeed! MessageBody:%s MessageID:%s" % (msg_body, re_msg.message_id) except MNSExceptionBase, e: if e.type == "QueueNotExist": print "Queue not exist, please create queue before send message." sys.exit(0) print "Send Message Fail! Exception:%s\n" % e
Step 4: Receive and delete the messages
Run the recvdelmessage.py file to receive and delete all the messages from the queue. You must specify the name of the queue that is created in Step 2 by using a specific parameter. The requests to receive messages from the queue are long polling requests. If you set the WaitSeconds parameter to 3, the long-polling request times out after 3 seconds. For more information, see QueueMessage.
If the SyntaxError: encoding problem: utf8
error message appears when you run the command, change carriage return (CR) and line feed (LF) characters and run the command again.
Run the following command:
python recvdelmessage.py MyQueue1
The following result is returned:
==========Receive And Delete Message From Queue========== QueueName:MyQueue1 WaitSeconds:3 Receive Message Succeed! ReceiptHandle:1-ODU4OTkzNDU5My0xNDYzNDcwNDU4LTEtOA== MessageBody:I am test message 0. MessageID:3EBE662B52BC99BC-1-154BD99CCA7-200000001 Delete Message Succeed! ReceiptHandle:1-ODU4OTkzNDU5My0xNDYzNDcwNDU4LTEtOA== Receive Message Succeed! ReceiptHandle:1-ODU4OTkzNDU5NC0xNDYzNDcwNDU4LTEtOA== MessageBody:I am test message 2. MessageID:3EBE662B52BC99BC-1-154BD99CCF0-200000002 Delete Message Succeed! ReceiptHandle:1-ODU4OTkzNDU5NC0xNDYzNDcwNDU4LTEtOA== Receive Message Succeed! ReceiptHandle:1-ODU4OTkzNDU5My0xNDYzNDcwNDU4LTItOA== MessageBody:I am test message 1. MessageID:64B92941FC57837F-2-154BD99CCCE-200000001 Delete Message Succeed! ReceiptHandle:1-ODU4OTkzNDU5My0xNDYzNDcwNDU4LTItOA== Queue is empty!
Sample code:
The endpoint, AccessKey ID, AccessKey secret, and STS token are retrieved from the sample.cfg file that is specified in Step 1.
my_account = Account(endpoint, accid, acckey, token) queue_name = sys.argv[1] if len(sys.argv) > 1 else "MySampleQueue" my_queue = my_account.get_queue(queue_name) wait_seconds = 3 print "%sReceive And Delete Message From Queue%s\nQueueName:%s\nWaitSeconds:%s\n" % (10*"=", 10*"=", queue_name, wait_seconds) while True: try: recv_msg = my_queue.receive_message(wait_seconds) print "Receive Message Succeed! ReceiptHandle:%s MessageBody:%s MessageID:%s" % (recv_msg.receipt_handle, recv_msg.message_body, recv_msg.message_id) except MNSExceptionBase,e: if e.type == "QueueNotExist": print "Queue not exist, please create queue before receive message." sys.exit(0) elif e.type == "MessageNotExist": print "Queue is empty!" sys.exit(0) print "Receive Message Fail! Exception:%s\n" % e continue try: my_queue.delete_message(recv_msg.receipt_handle) print "Delete Message Succeed! ReceiptHandle:%s" % recv_msg.receipt_handle except MNSException,e: print "Delete Message Fail! Exception:%s\n" % e
Step 5: Delete the queue
Run the deletequeue.py file to delete the queue.
Run the following command:
python deletequeue.py MyQueue1
The following result is returned:
Delete Queue Succeed! QueueName:MyQueue1
Sample code:
The endpoint, AccessKey ID, AccessKey secret, and STS token are retrieved from the sample.cfg file that is specified in Step 1.
my_account = Account(endpoint, accid, acckey, token) queue_name = sys.argv[1] if len(sys.argv) > 1 else "MySampleQueue" my_queue = my_account.get_queue(queue_name) try: my_queue.delete() print "Delete Queue Succeed! QueueName:%s\n" % queue_name except MNSExceptionBase, e: print "Delete Queue Fail! Exception:%s\n" % e