【问题标题】:Send/Receive an image with Python+Stomp.py and ActiveMQ使用 Python+Stomp.py 和 ActiveMQ 发送/接收图像
【发布时间】:2014-05-30 23:15:58
【问题描述】:

我正在尝试通过 ActiveMQ 使用 python + stomp.py 发送/接收图像。它从内存中的图像开始,从前端上传。一个脚本将其发送到 AMQ,另一个脚本从那里接收,写入文件并将链接返回到前端。

但是出了点问题 - 导致文件没有显示图片。接收到图像的文件几乎与原始文件大小相符,仅大 3-4 KB。但它什么也没显示。

我不知道会发生什么... AMQ 是在带有图像数据的消息中添加一些东西,还是什么?有什么想法吗?

监听器类代码:

class MyListener(object):
    msglist = []

    def __init__(self):
        self.msglist = []

    def on_error(self, headers, message):
        self.msglist.append('<[ERROR]> ' + message)

    def on_message(self, headers, message):
        self.msglist.append(message)

发送 IMG 消息代码:

if request.FILES.get('image2send'):
    img = request.FILES['image2send']
    conn = stomp.Connection()
    conn.set_listener('', MyListener())
    conn.start()
    conn.connect()
    conn.send(body=' '.join(img), destination='/queue/test_img', headers={'persistent': 'true'})
    time.sleep(2)
    conn.disconnect()

正在接收 IMG 消息代码:

lst = MyListener()
conn = stomp.Connection()
conn.set_listener('', lst)
conn.start()
conn.connect()
conn.subscribe(destination='/queue/test_img', id=1, ack='auto')
time.sleep(2)
conn.disconnect()
if len(lst.msglist) > 0:
    dest = open(MEDIA_ROOT + 'amq_getpic/thepic.png', 'wb+')
    dest.write(lst.msglist[0])
    dest.close()

【问题讨论】:

  • STOMP 和 AMQ 的有趣用法。

标签: python django image activemq stomp


【解决方案1】:

问题在于发送 IMG 消息代码

原始字符串:

conn.send(body=' '.join(img),
          destination='/queue/test_img',
          headers={'persistent': 'true'})

固定:

conn.send(body=''.join(img),
          destination='/queue/test_img', 
          headers={'persistent': 'true'})

发送的正文字符串中的空格正在破坏文件

【讨论】:

    猜你喜欢
    • 2012-01-02
    • 2019-08-03
    • 1970-01-01
    • 2020-04-23
    • 2010-10-19
    • 2015-01-08
    • 2017-10-24
    • 2016-05-02
    • 1970-01-01
    相关资源
    最近更新 更多