从SoftLayer消息队列中读取

时间:2014-01-21 15:35:17

标签: python-2.7 message-queue

我正在使用SoftLayer的Message Queue服务。我创建了一个队列,其中包含一些消息,我正在尝试从Python脚本中读取它们。

我安装了以下Python包https://github.com/softlayer/softlayer-message-queue-python。以下是我的测试文件的样子:

from softlayer_messaging.queue import Queue, Message
from softlayer_messaging.compat import json
from softlayer_messaging.auth import QueueAuth
from softlayer_messaging.errors import Unauthenticated

if __name__ == '__main__':

    endpoint = "{I'm using the public host from my Message Queue credentials}"
    username = "{My username}"
    api_key = "{My API account key}"
    auth_token = None

    auth = QueueAuth(
            endpoint, username, api_key,
            auth_token=auth_token)

    auth.auth()

    queue = Queue(endpoint, "domains", auth)
    print queue.detail()

如果我在调用 auth.auth()方法后显示auth_token,那么它会显示一个随机字符串,所以我相信身份验证是完全正确的。

问题是 queue.detail()方法抛出以下异常:

File "test1.py", line 23, in <module>
    print queue.detail()
  File "/usr/local/lib/python2.7/site-packages/softlayer_messaging/queue.py", line 24, in detail
    return self.get().json
  File "/usr/local/lib/python2.7/site-packages/softlayer_messaging/resource.py", line 23, in get
    return self.request('GET', *args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/softlayer_messaging/queue.py", line 20, in request
    resp.raise_for_status()
  File "/usr/local/lib/python2.7/site-packages/softlayer_messaging/resource.py", line 84, in raise_for_status
    raise ResponseError("%s: %s" % (code, message), errors)
softlayer_messaging.errors.ResponseError: 404: Not found

我能够连接到队列并从PHP脚本中读取消息,所以我很确定问题不在于队列本身。关于我做错了什么想法?

非常感谢你的帮助。

1 个答案:

答案 0 :(得分:1)

我能够弄明白,这是我的代码:

import softlayer_messaging

if __name__ == '__main__':

    client = softlayer_messaging.get_client('{queue_account_id}', endpoint='https://dal05.mq.softlayer.net/')
    client.authenticate('{username}', '{api_key}')

    messages = client.queue('{queue_name}').pop(10)

    if 'item_count' in messages and 'items' in messages and messages['item_count'] > 0:
        for message in messages['items']:

            print message['body']

对于其他端点,可以在API reference guide中找到更多示例。