コード例 #1
0
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:
        msg_body = u"I am test message %s." % i
        msg = TopicMessage(msg_body)
        re_msg = my_topic.publish_message(msg)
コード例 #2
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)
コード例 #3
0
ファイル: subscribe.py プロジェクト: cloudmodal/cloudhelper
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_topic import *
from mns.mns_subscription import *

#参数合法性检查,订阅的Endpoint参数必须传入
sub_endpoint = MNSSampleCommon.LoadIndexParam(1)
if not sub_endpoint:
    print(
        "Please specify endpoint. e.g. python subscribe.py http://127.0.0.1:80"
    )
    sys.exit(1)

#从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_sub
my_account = Account(endpoint, accid, acckey, token)

topic_name = MNSSampleCommon.LoadIndexParam(2)
if not topic_name:
    topic_name = "MySampleTopic"
my_topic = my_account.get_topic(topic_name)

sub_name = MNSSampleCommon.LoadIndexParam(3)
コード例 #4
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.mns_account import Account
from mns.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,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)
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 as 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)
コード例 #5
0
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)
my_queue.set_encoding(base64)

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