<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; color: rgb(0, 0, 0); font-size: 14px; font-family: Calibri, sans-serif; "><div>Here's a straightforward publisher using pika async library.</div><ol><li>Be sure you are using pika 0.9.6 (released about a week ago).</li><li>Create an exchange 'my_log_exchange' in vhost '/'. This and the step below could be done in code � eliminated for simplicity.</li><li>Create a queue '&lt;whatever&gt;' in vhost '/' and bind it to the above exchange with key '#' (to get everything)</li><li>Create a file named 'my_id' in the same directory (or findable) by the code below. Put some text in it.</li><li>Run the program. Look in the queue. Get your message as JSON with the body attribute set to the text.</li></ol><div>I am using python 2.7 but this should work with 2.6. <a href="http://pika.readthedocs.org">http://pika.readthedocs.org</a>/ has good examples too.</div><div><br></div><div>Michael</div><div><br></div><div><div><span style="font-family: Courier; ">#!/usr/bin/env python</span></div><div><span style="font-family: Courier; "># -*- coding: utf-8 -*-</span></div><div><span style="font-family: Courier; "><br></span></div><div><span style="font-family: Courier; ">import sys</span></div><div><span style="font-family: Courier; ">import json</span></div><div><span style="font-family: Courier; "><br></span></div><div><span style="font-family: Courier; ">import pika</span></div><div><span style="font-family: Courier; "><br></span></div><div><span style="font-family: Courier; ">class BasicPublish(object):</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; def __init__(self):</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; self._properties = pika.BasicProperties(content_type=&quot;application/json&quot;, user_id='guest')</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; self._credentials = pika.PlainCredentials('guest', 'guest')</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; self._connection = None</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; self._channel = None</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; def put_log(self):</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; print 'put_message'</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; key = 'my_id'</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; body = self.get_body(key)</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; message = dict(body=body)</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; self.put_message(key, message)</span></div><div><span style="font-family: Courier; "><br></span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; def get_body(self, key):</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; print 'get_body'</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; body = self.get_file_contents(key) &nbsp; &nbsp; &nbsp; &nbsp;</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; return body</span></div><div><span style="font-family: Courier; "><br></span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; def get_file_contents(self, fname):</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; print 'get_file_contents'</span></div><div><span style="font-family: Courier; "><br></span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; with open(fname) as f:</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return f.read()</span></div><div><span style="font-family: Courier; "><br></span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; def put_message(self, key, message):</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; print 'put_message'</span></div><div><span style="font-family: Courier; "><br></span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; self._channel.basic_publish(</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exchange='my_log_exchange',</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; routing_key=key,</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; body=json.dumps(message),</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; properties=self._properties</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; )</span></div><div><span style="font-family: Courier; "><br></span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; def channel_on_open_callback(self, channel):</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; print 'channel_on_open_callback: channel:{0}'.format(channel)</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; self._channel = channel</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; self.put_log() &nbsp; &nbsp; &nbsp;</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; def connection_callback(self, connection):</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; print 'connection_callback'</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; self._connection.channel(on_open_callback=self.channel_on_open_callback)</span></div><div><span style="font-family: Courier; "><br></span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; def connection(self):</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; print 'connection'</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; parameters = pika.ConnectionParameters(</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; virtual_host='/',</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; credentials=self._credentials</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; )</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; self._connection = pika.SelectConnection(parameters, self.connection_callback)</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; def connection_close(self):</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; self._connection.close()</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; def stop(self): # called on interrupt</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; self.connection_close()</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; self._connection.ioloop.start() # stopped by interrupt - must be restarted to process commands</span></div><div><span style="font-family: Courier; "><br></span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; def run(self):</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; print 'run'</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; self.connection()</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; self._connection.ioloop.start()</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span></div><div><span style="font-family: Courier; ">def main(argv=None):</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; if argv is None: argv = sys.argv</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; basic_publish = BasicPublish()</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp;&nbsp;</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; try:</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; basic_publish.run()</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; except KeyboardInterrupt:</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; print &gt;&gt;sys.stderr, &quot;Interrupt!&quot;</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; basic_publish.stop()</span></div><div><span style="font-family: Courier; "><br></span></div><div><span style="font-family: Courier; ">if __name__ == &quot;__main__&quot;:</span></div><div><span style="font-family: Courier; ">&nbsp; &nbsp; sys.exit(main())</span></div></div><div><br></div><div><br></div><span id="OLK_SRC_BODY_SECTION"><div style="font-family:Calibri; font-size:11pt; text-align:left; color:black; BORDER-BOTTOM: medium none; BORDER-LEFT: medium none; PADDING-BOTTOM: 0in; PADDING-LEFT: 0in; PADDING-RIGHT: 0in; BORDER-TOP: #b5c4df 1pt solid; BORDER-RIGHT: medium none; PADDING-TOP: 3pt"><span style="font-weight:bold">From: </span> Jerry Kuch &lt;<a href="mailto:jerryk@rbcon.com">jerryk@rbcon.com</a>&gt;<br><span style="font-weight:bold">Reply-To: </span> rabbitmq &lt;<a href="mailto:rabbitmq-discuss@lists.rabbitmq.com">rabbitmq-discuss@lists.rabbitmq.com</a>&gt;<br><span style="font-weight:bold">Date: </span> Sun, 4 Nov 2012 13:24:38 -0500<br><span style="font-weight:bold">To: </span> rabbitmq &lt;<a href="mailto:rabbitmq-discuss@lists.rabbitmq.com">rabbitmq-discuss@lists.rabbitmq.com</a>&gt;<br><span style="font-weight:bold">Subject: </span> Re: [rabbitmq-discuss] Searching for a client library/example: Problems getting a reliable connection in Python and Erlang<br></div><div><br></div>Also, further to what Alvaro said: &nbsp;The Erlang client is quite reliable and, indeed, is used by us internally for a range of things. &nbsp;Its main downside is that the documentation can be a bit sparse, especially if you're new to Erlang, and it has had some API changes over the years that have rendered some of what you might read about it on the internet out of date (again, in ways that are probably harder to sort out if you're new to the whole mess)...<div><br></div><div>&#43;1 to Alvaro's request for some more specifics on what you're up to helping us provide guidance... :-)</div><div><br></div><div>Best regards,</div><div>Jerry<br><br><div class="gmail_quote">On Sun, Nov 4, 2012 at 10:20 AM, Alvaro Videla <span dir="ltr">&lt;<a href="mailto:videlalvaro@gmail.com" target="_blank">videlalvaro@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Would you mind sharing some code? Is hard for us to help you without code.<br><br>
Regards,<br><br>
Alvaro<br><br>
Sent from my iPhone<br><div class="HOEnZb"><div class="h5"><br>
On Nov 4, 2012, at 7:06 PM, Daniel Neugebauer &lt;<a href="mailto:mailinglists@energiequant.de">mailinglists@energiequant.de</a>&gt; wrote:<br><br>
&gt; Hi!<br>
&gt;<br>
&gt; I wanted to use RabbitMQ for simple log collection but for some reason I<br>
&gt; can't find any useful AMQP client library (and what good is a server<br>
&gt; without a working client?). I'd like to implement in either Python or<br>
&gt; Erlang but I'm unable to get a reliable connection - either connection<br>
&gt; setup fails, strange error messages appear after a short uptime or the<br>
&gt; client just cannot handle connection loss.<br>
&gt;<br>
&gt; What I've written so far is a simple Python script that takes lines from<br>
&gt; stdin, builds a dictionary from it and tries to send it via amqplib,<br>
&gt; Pika or Kombu (which seems to decide on using amqplib internally). It's<br>
&gt; nothing special, I already have a serialized string, that's all I need<br>
&gt; to send.<br>
&gt;<br>
&gt; However, Pika or amqplib (it's been a while since I tried them, so I<br>
&gt; don't remember which one it was) appeared to have some buffer issue -<br>
&gt; although the connection (to localhost) was still alive, I would get<br>
&gt; error messages about messages queueing up in the client after a few<br>
&gt; minutes. The other library would loose connection a few seconds after<br>
&gt; connecting but doesn't notice. Kombu terminates when the connection to<br>
&gt; RabbitMQ is lost, although I would expect Kombu to retry since I set<br>
&gt; retry = True when calling publish on the Producer object. I'm already<br>
&gt; running a reconnect in a loop with a try/except inside but my script<br>
&gt; just terminates. Strange...<br>
&gt;<br>
&gt; I also tried to use amqp_client in Erlang but failed to get any<br>
&gt; connection (I always get unknown_host unless I enter nothing as host<br>
&gt; name). I should add that I only read into Erlang theoretically so far<br>
&gt; and didn't get to write much but example programs yet, so it's likely I<br>
&gt; do something wrong here (yes, I already tried both binary lists and<br>
&gt; regular lists, no difference; I even tried supplying IP addresses in<br>
&gt; various forms - without success). Documentation is scarce and what I<br>
&gt; found suggests that I'm doing nothing wrong. I would prefer staying with<br>
&gt; Python for this implementatin anyway, just thought the client that comes<br>
&gt; with RabbitMQ should be able to work fine - unfortunately I seem to be<br>
&gt; wrong here. :(<br>
&gt;<br>
&gt; My question is: Does anyone have a complete working example of a<br>
&gt; *reliable* client in Python (or Erlang)? Maybe I'm too stupid to read<br>
&gt; the (very brief?) documentation correctly or the libraries I tried so<br>
&gt; far are just too buggy... What I started with was an example from<br>
&gt; &quot;RabbitMQ in Action&quot; but even that didn't work as expected.<br>
&gt;<br>
&gt; If it wasn't for getting logs piped in on the console I would try Java<br>
&gt; clients instead as I expect them to be more mature - to me, the Python<br>
&gt; libraries appear to be a bit messy, pardon my (maybe wrong) impression<br>
&gt; but I'm starting to get very frustrated with this: I've never had more<br>
&gt; problems to just get a simple plain string handed over to some other<br>
&gt; system. All in all I've spent too many hours in trying to get anything<br>
&gt; working and nothing has worked so far.<br>
&gt;<br>
&gt; Thanks,<br>
&gt; Daniel<br>
&gt; _______________________________________________<br>
&gt; rabbitmq-discuss mailing list<br>
&gt; <a href="mailto:rabbitmq-discuss@lists.rabbitmq.com">rabbitmq-discuss@lists.rabbitmq.com</a><br>
&gt; <a href="https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss" target="_blank">https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss</a><br>
_______________________________________________<br>
rabbitmq-discuss mailing list<br><a href="mailto:rabbitmq-discuss@lists.rabbitmq.com">rabbitmq-discuss@lists.rabbitmq.com</a><br><a href="https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss" target="_blank">https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss</a><br></div></div></blockquote></div><br></div></span></body></html>