本文介绍使用Python SDK发送SMQ消息时的准备工作及示例代码。
前提条件
- 重要
本文代码中提及的endpoint从配置访问域名时的sample.cfg文件中读取,accid、acckey和 token从环境变量中读取。
示例代码下载,请参见示例代码。
消息体编码选择
当消息体无特殊字符时,建议您不使用Base64编码。
发送消息时使用
message.setMessageBodyAsRawString
方法设置消息体。接收消息时使用
meesage.getMessageBodyAsRawString
方法获取消息体。
发送消息
运行
sendmessage.py
,命令如下:python sendmessage.py MyQueue1
返回结果如下:
==========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
核心代码:
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 = u"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 ReceiptHandle:%s" % (msg_body, re_msg.message_id, re_msg.receipt_handle)) except MNSExceptionBase as 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)
接收和删除消息
运行
recvdelmessage.py
,命令如下:python recvdelmessage.py MyQueue1
返回结果如下:
==========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!
核心代码:
my_account = Account(endpoint, accid, acckey, token) queue_name = sys.argv[1] if len(sys.argv) > 1 else "MySampleQueue" base64 = False if len(sys.argv) > 2 and sys.argv[2].lower() == "false" else True my_queue = my_account.get_queue(queue_name) my_queue.set_encoding(base64) 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: // receive_message 返回消息的消息体为字节串,receive_message_with_str_body 返回消息的消息体为字符串 // recv_msg = my_queue.receive_message(wait_seconds) recv_msg = my_queue.receive_message_with_str_body(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 Exception as e: if hasattr(e, 'type'): if e.type == u"QueueNotExist": print("Queue not exist, please create queue before receive message.") sys.exit(0) elif e.type == u"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 Exception as e: print("Delete Message Fail! Exception:%s\n" % e)