Search code examples
pythonamqppika

Leading b in python pika response


I am trying to make a simple AMQP client using python. I copied the code I found in RabbitMQ website:

#!/usr/bin/env python
import pika

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

channel.queue_declare(queue='hello')

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

channel.basic_consume(callback,
                  queue='hello',
                  no_ack=True)

print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()

This works except It always prints something like [x] Received b'my message'. Due to this I cant parse my json messages. How do I fix this ?


Solution

  • You may use decode() to convert the string to utf-8 and then print it out, something like

    str = 'your str'
    print(str.decode())