Question

I'm trying to send/receive an image with python + stomp.py via ActiveMQ. It starts with an image in memory, uploaded from frontend. One script sends it to AMQ, and another receives from there, writes to a file and returns the link to frontend.

But something goes wrong - cause the file doesn't show the picture. A file with received image almost fits by the size the original one, only 3-4 Kbytes bigger. But it doesn't show anything.

And i can't get what happens... does AMQ add something to a message with an image data, or what? Any ideas?

LISTENER CLASS CODE:

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)

SENDING IMG MESSAGE CODE:

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()

RECEIVING IMG MESSAGE CODE:

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()
Was it helpful?

Solution

The problem was in the SENDING IMG MESSAGE CODE.

original string:

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

fixed:

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

a space in the sent body string is corrupting the file

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top