2014-04-16 33 views
1

我試圖通過ActiveMQ發送/接收圖像python + stomp.py。它從內存中的圖像開始,從前端上傳。一個腳本將其發送到AMQ,另一個腳本從那裏接收,寫入文件並將鏈接返回到前端。用Python + Stomp.py和ActiveMQ發送/接收圖像

但出現問題 - 導致文件不顯示圖片。收到圖像的文件幾乎與原始文件大小相符,只有3-4千字節大。但它沒有顯示任何東西。

而我不能得到什麼情況...... AMQ是否會在圖像數據中添加某些內容或者什麼?有任何想法嗎?

LISTENER類代碼:

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消息的代碼:

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消息的代碼:

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

STOMP和AMQ的有趣的用法。 – Buchi

回答

1

問題出在SENDING IMG MESSAGE CODE

原始字符串:

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

固定:

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

在發送體中的字符串的空間被破壞文件