<div><br></div><div><div class="gmail_extra"><div class="gmail_quote">On Mon, Nov 19, 2012 at 1:51 PM, Anthony Suh <span dir="ltr"><<a href="mailto:suhprano@gmail.com" target="_blank">suhprano@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Hello, I'm having trouble retrieving specific values (headers) from the basicproperties list.</blockquote>
<div><br></div><div><div>Hi,</div><div><br></div><div>The headers property is an attribute of the property object and is a dictionary. Here's an example publisher and consumer:</div><div><br></div><b>publish.py:</b><div>
<br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><span style="font-family:'courier new',monospace">import pika</span></div><div><font face="courier new, monospace">import time</font></div>
<div><span style="font-family:'courier new',monospace"><br></span></div><div><span style="font-family:'courier new',monospace">connection = pika.BlockingConnection()</span></div><div><span style="font-family:'courier new',monospace">channel = connection.channel()</span></div>
<div><span style="font-family:'courier new',monospace">for x in range(0, 100):</span></div><div><font face="courier new, monospace"> channel.basic_publish(exchange='test',</font></div><div><font face="courier new, monospace"> routing_key='test',</font></div>
<div><font face="courier new, monospace"> body='Hello World!',</font></div><div><font face="courier new, monospace"> properties=pika.BasicProperties(content_type='text/plain',</font></div>
<div><font face="courier new, monospace"> app_id='test',</font></div><div><font face="courier new, monospace"> headers={'foo': 'bar', 'baz': 'quux'},</font></div>
<div><font face="courier new, monospace"> timestamp=int(time.time()),</font></div><div><font face="courier new, monospace"> delivery_mode=1))</font></div>
</blockquote><div><div><br></div><div><b>consume.py:</b></div><div><br></div></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><font face="courier new, monospace">import pika</font></div><div>
<font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">connection = pika.BlockingConnection()</font></div><div><font face="courier new, monospace">channel = connection.channel()</font></div>
<div><span style="font-family:'courier new',monospace">for method_frame, properties, body in channel.consume('test_queue'):</span></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"> # Display the message parts</font></div>
<div><font face="courier new, monospace"> print 'Routing Key: %s' % method_frame.routing_key</font></div><div><font face="courier new, monospace"> print 'Headers: %r' % properties.headers</font></div>
<div><font face="courier new, monospace"> print 'Foo header: %s' % properties.headers.get('foo')</font></div><div><font face="courier new, monospace"> print 'Body: %s' % body</font></div><div>
<font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"> # Acknowledge the message</font></div><div><font face="courier new, monospace"> channel.basic_ack(method_frame.delivery_tag)</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"> # Break when 1 message was received</font></div><div><font face="courier new, monospace"> if method_frame.delivery_tag == 1:</font></div>
<div><font face="courier new, monospace"> break</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"># Cancel the consumer and return any pending messages</font></div>
<div><font face="courier new, monospace">requeued_messages = channel.cancel()</font></div><div><font face="courier new, monospace">print 'Requeued %i messages' % requeued_messages</font></div><div><font face="courier new, monospace"><br>
</font></div><div><font face="courier new, monospace"># Close the channel and the connection</font></div><div><font face="courier new, monospace">channel.close()</font></div><div><font face="courier new, monospace">connection.close()</font></div>
</blockquote><div><div><br></div><div><b>Output:</b></div><div><br></div></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><font face="courier new, monospace">Routing Key: test<br></font></div>
<div><div><font face="courier new, monospace">Headers: {'foo': 'bar', 'baz': 'quux'}</font></div><div><font face="courier new, monospace">Body: Hello World!</font></div><div><font face="courier new, monospace">Foo header: bar<br>
</font></div><div><font face="courier new, monospace">Requeued 266 messages</font></div></div></blockquote><div><div class="gmail_extra"><br>Hope this helps,</div><div class="gmail_extra"><br>Gavin</div></div></div><div>
<br>
</div><div><br></div><div> </div></div>
</div></div>