使用Python博托与AWS SQS,找回了无意义的字符 [英] Using python BOTO with AWS SQS, getting back nonsense characters

查看:533
本文介绍了使用Python博托与AWS SQS,找回了无意义的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我使用Python和博托访问我的AWS SQS。我在赛强,我可以从AWS仪表盘看到一些消息。但是,当我试图打通蟒蛇这些消息,那来通过人物都只是乱码。任何想法是怎么回事?

 康恩= boto.sqs.connect_to_region(美东-1)
Q = conn.get_queue(我的工人队列)
打印q
从消息队列#read
消息= q.read(60)
打印消息
打印message.get_body()
 

由于code以上,我得到如下:

 队列(https://queue.amazonaws.com/247124526695/my-worker-queue)
<在0x16f31b8&GT boto.sqs.message.Message实例;
??ê?? B + ??? - 
 

在消息队列中的文字是这样的:

 您好,这是一个测试
 

解决方案

我想原因是的base64 解码的问题,因为博托使用的base64 消息编码和解码。你可以尝试使用<一个href="http://boto.s3.amazonaws.com/ref/sqs.html#boto.sqs.message.RawMessage.get_body_en$c$cd"><$c$c>get_body_en$c$cd方法:

 打印message.get_body_en codeD()
 

另一种选择是转换成 RawMessage

 从boto.sqs.message进口RawMessage
q.set_message_class(RawMessage)
 

更新

的确是这样,很显然你的测试用例:

 &GT;&GT;&GT;打印您好,这是一个test'.de code('的base64)
??ê?? B + ??? - 
 

So, I am using python and BOTO to access my AWS SQS. I have some messages in the SQS which I can see from the AWS dashboard. However, when I try to get these messages through python, the characters that come through are just gibberish. Any idea what is going on here?

conn = boto.sqs.connect_to_region("us-east-1") 
q = conn.get_queue('my-worker-queue')
print q 
#read from message queue
message = q.read(60)
print message
print message.get_body()

Given the code above, I get the following:

Queue(https://queue.amazonaws.com/247124526695/my-worker-queue)
<boto.sqs.message.Message instance at 0x16f31b8>
??e??b?+??-

The text in the message queue is the following:

hello this is a test

解决方案

I guess the reason is base64 decoding issue, as boto uses base64 for messages encoding and decoding. You can try to use get_body_encoded method:

print message.get_body_encoded()

Other option is convert to RawMessage:

from boto.sqs.message import RawMessage
q.set_message_class(RawMessage)

Update

Yes it is, it became clear with your test case:

>>> print 'hello this is a test'.decode('base64')
??e??b?+??-

这篇关于使用Python博托与AWS SQS,找回了无意义的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆