0

我正在使用 boto 库从 SQS 队列中读取消息。我的消息有这样的文字: { Command:XXXXXXXXXXX Key:XXXXXXX Input:XXXXXX}。Boto 使用 base64 编码发送并读取它,因此如果我读取消息正文,那么文本就在那里。但是我怎么能读到像这样的消息

Command = input['Command'] 
Key = input_message['Key'].split(',')

这样我就可以使用这些值进行进一步处理...

我对 Python 也很陌生

4

2 回答 2

1

Ok, you seem to have the input in some kind of a format - is it anything standardised? If not, you would need to parse the contents of your message and get the individual keys.

What I have been doing before in my projects was using JSON to facilitate data exchange between platforms.

If you do not have a luxury to edit your incoming data, you would need to do something like this (very naiive example):

input = "{ Command:XXXXXXXXXXX Key:XXXXXXX Input:XXXXXX }"
data = filter(lambda x: ":" in x, input.split())
message_dict = dict()
for item in data:
    key, val = item.split(":")
    message_dict[key] = val
于 2012-10-31T15:34:42.013 回答
0

考虑使用老式的 JSON 轻松地通过网络轻松发送和接收字典。

这个测试函数用 JSON 验证数据格式非常清晰:

test_sqs.py

import json

import boto3
from moto import mock_sqs

@mock_sqs
def test_sqs():
    sqs = boto3.resource('sqs', 'us-east-1')
    queue = sqs.create_queue(QueueName='votes')

    queue.send_message(MessageBody=json.dumps(
        {'Command': 'drink', 'Key': 'beer', 'Input': 'tasty'}))

    messages = queue.receive_messages()
    assert len(messages) == 1
    assert messages[0].body == (
        '{"Input": "tasty", "Command": "drink", "Key": "beer"}')
于 2016-08-13T20:26:24.193 回答