The example of "Hello World" using pika does not mention the message's format is byte stream

1,729 views
Skip to first unread message

Rick Hunter

unread,
Jun 15, 2017, 6:32:46 AM6/15/17
to rabbitmq-users
def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)

channel.basic_consume(callback,
                      queue='hello',
                      no_ack=True)
The body parameter is a byte array actually.
When we print the body as following
print("Receive the message", body)
The output is 
Receive the message b"Test Message"

If we want to use this parameter to compare, we need convert it to str type.
bodyMessage = str(body, "utf-8") //the encoding is the message's encoding.
 Then we can compare correctly.
if bodyMessage =="xxxx":
     
//do some action

















Michael Klishin

unread,
Jun 15, 2017, 11:10:08 AM6/15/17
to rabbitm...@googlegroups.com
Hi Rick,

Thank you for your feedback.

Our tutorials are open source and you are more than welcome
to contribute a change:


(note the branch I've linked to in the website repo)

One question I have is: does this work the same way for Python 2.6 and 2.7?
We can require 2.7 for the tutorials and support 3.x but it would be nice to keep 2.6
compatibility if possible.

Cheers.


--
You received this message because you are subscribed to the Google Groups "rabbitmq-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-users+unsubscribe@googlegroups.com.
To post to this group, send email to rabbitmq-users@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
MK

Staff Software Engineer, Pivotal/RabbitMQ

Rick Hunter

unread,
Jun 18, 2017, 11:33:00 PM6/18/17
to rabbitmq-users
Dear Michael,
     I have tried this example using Python 2.7.5 in CentOS 7. The input string can be received successfully.
    



   


But when I tried using Python 3.6, this problem still exists.
   
   
  







Then sender's source code is:
message = "quit"
messageProps = pika.BasicProperties()
messageProps.content_type="text/plain"
messageProps.content_encoding="utf-8"
messageProps.delivery_mode=2
channel.basic_publish(exchange="", routing_key="xxxx", body=message, properties=messageProps)

2017年6月15日星期四 UTC+8下午11:10:08,Michael Klishin写道:
To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-user...@googlegroups.com.
To post to this group, send email to rabbitm...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Michael Klishin

unread,
Jun 18, 2017, 11:43:34 PM6/18/17
to rabbitm...@googlegroups.com
Hi Rick,

We'd consider a pull request that makes encoding/rendering of output values identical for
Python 2 and 3. We have to support 2.7 but ideally would like to also support 2.6.

Please see the two repos where we have tutorial code in my earlier response in this thread:

Thank you.


To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-users+unsubscribe@googlegroups.com.
To post to this group, send email to rabbitmq-users@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Rick Hunter

unread,
Jun 18, 2017, 11:52:00 PM6/18/17
to rabbitmq-users
Dear Michael,
      I will read the tutorial code. But I still have one problem, why the byte stream can be recognized as string in Python 2, but cannot be recognized in Python 3?

在 2017年6月19日星期一 UTC+8上午11:43:34,Michael Klishin写道:

Luke Bakken

unread,
Jun 19, 2017, 9:37:46 AM6/19/17
to rabbitmq-users
Rick -

This is a key difference between Python 2 and 3 and will affect much more in your Python application than just it's use of Pika.


We may decide to use the value of content_encoding to decode the message payload for certain encoding values.
 
Luke

Michael Klishin

unread,
Jun 19, 2017, 4:55:02 PM6/19/17
to rabbitm...@googlegroups.com
As Luke explains, Pika doesn't do that.

Message payloads are generally treated as opaque bags of bytes by client libraries,
as encoding details is a runtime and application-specific concern.

Generally we recommend using a data encoding library or widely used format (JSON, MsgPack,
Protocol Buffers, Kryo, and so on) instead of strings for message payloads. Those
formats will provide your payload both structure and interoperability (and in many cases,
the degree of efficiency that custom application-specific parsers may or may not achieve).

Of course, the tutorials try to explain as little as possible of those additional and system-specific
details so we use text strings in payloads.

--
You received this message because you are subscribed to the Google Groups "rabbitmq-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-users+unsubscribe@googlegroups.com.
To post to this group, send email to rabbitmq-users@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Rick Hunter

unread,
Jun 19, 2017, 10:12:33 PM6/19/17
to rabbitmq-users
Dear Michael and Luke,
      Thank you for your explanation. I will try to use the libraries you recommended.

在 2017年6月20日星期二 UTC+8上午4:55:02,Michael Klishin写道:
As Luke explains, Pika doesn't do that.

Message payloads are generally treated as opaque bags of bytes by client libraries,
as encoding details is a runtime and application-specific concern.

Generally we recommend using a data encoding library or widely used format (JSON, MsgPack,
Protocol Buffers, Kryo, and so on) instead of strings for message payloads. Those
formats will provide your payload both structure and interoperability (and in many cases,
the degree of efficiency that custom application-specific parsers may or may not achieve).

Of course, the tutorials try to explain as little as possible of those additional and system-specific
details so we use text strings in payloads.
On Mon, Jun 19, 2017 at 4:37 PM, Luke Bakken <lba...@pivotal.io> wrote:
Rick -

This is a key difference between Python 2 and 3 and will affect much more in your Python application than just it's use of Pika.


We may decide to use the value of content_encoding to decode the message payload for certain encoding values.
 
Luke

On Sunday, June 18, 2017 at 8:52:00 PM UTC-7, Rick Hunter wrote:
Dear Michael,
      I will read the tutorial code. But I still have one problem, why the byte stream can be recognized as string in Python 2, but cannot be recognized in Python 3?

--
You received this message because you are subscribed to the Google Groups "rabbitmq-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-user...@googlegroups.com.
To post to this group, send email to rabbitm...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages