Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Leading b in python pika response

Tags:

python

amqp

pika

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 ?

like image 625
Kamrul Khan Avatar asked Mar 21 '16 03:03

Kamrul Khan


1 Answers

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

str = 'your str'
print(str.decode())
like image 84
yichucai Avatar answered Oct 21 '22 01:10

yichucai