Python Pika Unicode error

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Python Pika Unicode error

9ners
getting the following while trying to receive raw data (from UDP source routed to rabbitmq exchange). Text or Syslog format works alright but errors out with raw message types. Please help

Exit press CTRL+C
Traceback (most recent call last):
  File "receive_logs.py", line 27, in <module>
    channel.start_consuming()
  File "/usr/lib/python2.6/site-packages/pika/adapters/blocking_connection.py", line 771, in start_consuming
    self.connection.process_data_events()
  File "/usr/lib/python2.6/site-packages/pika/adapters/blocking_connection.py", line 129, in process_data_events
    if self._handle_read():
  File "/usr/lib/python2.6/site-packages/pika/adapters/blocking_connection.py", line 232, in _handle_read
    super(BlockingConnection, self)._handle_read()
  File "/usr/lib/python2.6/site-packages/pika/adapters/base_connection.py", line 307, in _handle_read
    self._on_data_available(data)
  File "/usr/lib/python2.6/site-packages/pika/connection.py", line 1141, in _on_data_available
    consumed_count, frame_value = self._read_frame()
  File "/usr/lib/python2.6/site-packages/pika/connection.py", line 1208, in _read_frame
    return frame.decode_frame(self._frame_buffer)
  File "/usr/lib/python2.6/site-packages/pika/frame.py", line 240, in decode_frame
    method.decode(frame_data, 4)
  File "/usr/lib/python2.6/site-packages/pika/spec.py", line 1902, in decode
    self.routing_key = encoded[offset:offset + length].decode('utf8')
  File "/usr/lib64/python2.6/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode bytes in position 26-27: invalid continuation byte


Code :
#!/usr/bin/env python
import pika
import sys

connection = pika.BlockingConnection(pika.ConnectionParameters(
        host='localhost'))
channel = connection.channel()

#channel.exchange_declare(exchange='topic_logs',type='topic')

result = channel.queue_declare(exclusive=False)
queue_name = result.method.queue

channel.queue_bind(exchange='collector.raw',
                       queue=queue_name,
                       routing_key="#")

print ' [*] Waiting for logs. To exit press CTRL+C'

def callback(ch, method, properties, body):
    print " [x] %r:%r" % (method.routing_key, body,)

channel.basic_consume(callback,
                      queue=queue_name,
                      no_ack=True)

channel.start_consuming()


Reply | Threaded
Open this post in threaded view
|

Re: Python Pika Unicode error

Tim Watson-6
Hi,

On 12 Mar 2013, at 22:16, 9ners wrote:
> getting the following while trying to receive raw data (from UDP source
> routed to rabbitmq exchange). Text or Syslog format works alright but errors
> out with raw message types. Please help
>

[snip]

>
> UnicodeDecodeError: 'utf8' codec can't decode bytes in position 26-27:
> invalid continuation byte
>

This indicates that the sender needs to ensure that the relevant parts of the payload (i.e., the routing key in this case) are encoded as valid UTF-8. Rabbit is not changing this, so you need to fix up the sender.

Cheers,
Tim
_______________________________________________
rabbitmq-discuss mailing list
[hidden email]
https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss