示例#1
0
def my_send_msg(num):

    # 从sample.cfg中读取基本配置信息
    ## WARNING: Please do not hard code your accessId and accesskey in next line.(more information: https://yq.aliyun.com/articles/55947)
    accid, acckey, endpoint, token = MNSSampleCommon.LoadConfig()

    # 初始化 my_account, my_queue
    my_account = Account(endpoint, accid, acckey, token)
    queue_name = "MyQueue1"
    my_queue = my_account.get_queue(queue_name)

    try:
        # msg_body = "I am test message %s." % i
        msg_body = num
        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
示例#2
0
def my_recv_msg():
    #从sample.cfg中读取基本配置信息
    ## WARNING: Please do not hard code your accessId and accesskey in next line.(more information: https://yq.aliyun.com/articles/55947)
    accid,acckey,endpoint,token = MNSSampleCommon.LoadConfig()

    #初始化 my_account, my_queue
    my_account = Account(endpoint, accid, acckey, token)
    queue_name = "MyQueue1"
    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)


    #循环读取删除消息直到队列空
    #receive message请求使用long polling方式,通过wait_seconds指定长轮询时间为3秒

    ## long polling 解析:
    ### 当队列中有消息时,请求立即返回;
    ### 当队列中没有消息时,请求在MNS服务器端挂3秒钟,在这期间,有消息写入队列,请求会立即返回消息,3秒后,请求返回队列没有消息;

    wait_seconds = 3
    # print "%sReceive And Delete Message From Queue%s\nQueueName:%s\nWaitSeconds:%s\n" % (10*"=", 10*"=", queue_name, wait_seconds)
    #读取消息
    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
topicName = '/us86YAUNjC1/ls1021a_perform/data'
queueName = 'aliyun-iot-us86YAUNjC1'

# Ali RDS server
#DB_HOST = "greengrass.co4tctnwmzmy.us-west-2.rds.amazonaws.com"
#DB_USERNAME = "******"
#DB_PASSWORD = "******"
DB_HOST = "47.95.232.88"
DB_USERNAME = "******"
DB_PASSWORD = "******"
DB_DATABASE = "greengrass"

# Read the basic configurations from sample.cfg
# WARNING: Please do not hard code your accessId and accesskey in next line.
# (more information: https://yq.aliyun.com/articles/55947)
accessKeyId, accesskeySecret, endpoint, token = MNSSampleCommon.LoadConfig()

# create the connect for IoT Kit
clt = client.AcsClient(accessKeyId, accesskeySecret, 'cn-shanghai')

# Init my_account, my_queue
my_account = Account(endpoint, accessKeyId, accesskeySecret, token)
queue_name = sys.argv[1] if len(sys.argv) > 1 else queueName
boolbase64 = 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(boolbase64)

# get the connection to Amazon Greengrass RDS
db = MySQLdb.connect(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE)
示例#4
0
#!/usr/bin/env python
#coding=utf8

import sys
import os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)) + "/..")

import time
from sample_common import MNSSampleCommon
from mns.account import Account
from mns.topic import *

#从sample.cfg中读取基本配置信息
## WARNING: Please do not hard code your accessId and accesskey in next line.(more information: https://yq.aliyun.com/articles/55947)
accid, acckey, endpoint, token = MNSSampleCommon.LoadConfig()

#初始化 my_account, my_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)

#创建主题, 具体属性请参考mns/topic.py中的TopicMeta结构
topic_meta = TopicMeta()
try:
    topic_url = my_topic.create(topic_meta)
    print("Create Topic Succeed! TopicName:%s\n" % topic_name)
except MNSExceptionBase, e:
    if e.type == "TopicAlreadyExist":
        print(
            "Topic already exist, please delete it before creating or use it directly."
        )
示例#5
0
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)) + "/..")

import time
from sample_common import MNSSampleCommon
from mns.account import Account
from mns.queue import *

#从sample.cfg中读取基本配置信息
## WARNING: Please do not hard code your accessId and accesskey in next line.(more information: https://yq.aliyun.com/articles/55947)
accid = 'LTAIf6atnIup14AK'
acckey = 'zfoeCi2Au0le7vUbsFOmEVtPrtlfZf'
endpoint = 'http://1771999519197750.mns.cn-hangzhou.aliyuncs.com/'
token = MNSSampleCommon.LoadConfig()

#初始化 my_account, my_queue
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)

#创建队列, 具体属性请参考mns/queue.py中的QueueMeta结构
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)