Ejemplo n.º 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
Ejemplo n.º 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)
Ejemplo n.º 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."
        )
Ejemplo n.º 5
0
#!/usr/bin/env python
# coding=utf8

import os
import sys

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

from sample_common import MNSSampleCommon
from mns.mns_account import Account
from mns.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 = MNSSampleCommon.LoadIndexParam(1)
if not topic_name:
    print("Error: get parameter failed")
    sys.exit(0)
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:
Ejemplo n.º 6
0
        logger.info(addr_info)
        httpd = HTTPServer((ip_addr, port), endpoint_class)
        httpd.socket = ssl.wrap_socket(httpd.socket,
                                       keyfile=tmpkeyfile,
                                       certfile=tmpcertfile,
                                       server_side=True)
        httpd.serve_forever()
        #httpd = server.ThreadedHTTPServer(('', port), endpoint_class)
    except KeyboardInterrupt:
        print("Shutting down the simple notify endpoint!")
        httpd.socket.close()


if __name__ == "__main__":

    ip_addr = MNSSampleCommon.LoadIndexParam(1)
    if not ip_addr:
        print("ERROR: Must specify IP address")
        sys.exit(0)

    para_port = MNSSampleCommon.LoadIndexParam(2)
    if para_port:
        port = int(para_port)
    else:
        port = 8080
    msg_type = MNSSampleCommon.LoadIndexParam(3)
    if not msg_type:
        msg_type = u"XML"

    main(ip_addr, port, server.SimpleHttpNotifyEndpoint, msg_type)
#!/usr/bin/env python
# coding=utf8

import os
import sys

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

from sample_common import MNSSampleCommon
from mns.mns_common import *
from mns.mns_account import Account
from mns.mns_topic import *
from mns.mns_subscription import *

ret, params = MNSSampleCommon.LoadParam(4)
if not ret:
    print(
        "Please specify endpoint. e.g. python subscribe_queueendpoint.py cn-hanghzou MySampleSubQueue"
    )
    sys.exit(1)

region = sys.argv[1]
queue_name = sys.argv[2]
topic_name = sys.argv[3]
sub_name = sys.argv[4]

# 从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()
account_id = endpoint.split("/")[2].split(".")[0]
queue_endpoint = TopicHelper.generate_queue_endpoint(region, account_id,
Ejemplo n.º 8
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.mns_account import Account
from mns.mns_queue import *
from mns.mns_exception 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_queue
my_account = Account(endpoint, accid, acckey, token)
queue_name = MNSSampleCommon.LoadIndexParam(1)
if not queue_name:
    print("Error: get parameter failed")
    sys.exit(0)

param_base64 = MNSSampleCommon.LoadIndexParam(2)
if param_base64 and param_base64 == u'true':
    base64 = True
else:
    base64 = False

my_queue = my_account.get_queue(queue_name)
Ejemplo n.º 9
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)