[rabbitmq-discuss] how to get headers from basicproperties list in python?
Gavin M. Roy
gmr at meetme.com
Mon Nov 19 23:05:47 GMT 2012
On Mon, Nov 19, 2012 at 1:51 PM, Anthony Suh <suhprano at gmail.com> wrote:
> Hello, I'm having trouble retrieving specific values (headers) from the
> basicproperties list.
Hi,
The headers property is an attribute of the property object and is a
dictionary. Here's an example publisher and consumer:
*publish.py:*
import pika
import time
connection = pika.BlockingConnection()
channel = connection.channel()
for x in range(0, 100):
channel.basic_publish(exchange='test',
routing_key='test',
body='Hello World!',
properties=pika.BasicProperties(content_type='text/plain',
app_id='test',
headers={'foo':
'bar', 'baz': 'quux'},
timestamp=int(time.time()),
delivery_mode=1))
*consume.py:*
import pika
connection = pika.BlockingConnection()
channel = connection.channel()
for method_frame, properties, body in channel.consume('test_queue'):
# Display the message parts
print 'Routing Key: %s' % method_frame.routing_key
print 'Headers: %r' % properties.headers
print 'Foo header: %s' % properties.headers.get('foo')
print 'Body: %s' % body
# Acknowledge the message
channel.basic_ack(method_frame.delivery_tag)
# Break when 1 message was received
if method_frame.delivery_tag == 1:
break
# Cancel the consumer and return any pending messages
requeued_messages = channel.cancel()
print 'Requeued %i messages' % requeued_messages
# Close the channel and the connection
channel.close()
connection.close()
*Output:*
Routing Key: test
Headers: {'foo': 'bar', 'baz': 'quux'}
Body: Hello World!
Foo header: bar
Requeued 266 messages
Hope this helps,
Gavin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20121119/a0857947/attachment.htm>
More information about the rabbitmq-discuss
mailing list