<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 '<whatever>' 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; "> def __init__(self):</span></div><div><span style="font-family: Courier; "> self._properties = pika.BasicProperties(content_type="application/json", user_id='guest')</span></div><div><span style="font-family: Courier; "> self._credentials = pika.PlainCredentials('guest', 'guest')</span></div><div><span style="font-family: Courier; "> self._connection = None</span></div><div><span style="font-family: Courier; "> self._channel = None</span></div><div><span style="font-family: Courier; "> </span></div><div><span style="font-family: Courier; "> def put_log(self):</span></div><div><span style="font-family: Courier; "> print 'put_message'</span></div><div><span style="font-family: Courier; "> key = 'my_id'</span></div><div><span style="font-family: Courier; "> body = self.get_body(key)</span></div><div><span style="font-family: Courier; "> message = dict(body=body)</span></div><div><span style="font-family: Courier; "> self.put_message(key, message)</span></div><div><span style="font-family: Courier; "><br></span></div><div><span style="font-family: Courier; "> def get_body(self, key):</span></div><div><span style="font-family: Courier; "> print 'get_body'</span></div><div><span style="font-family: Courier; "> body = self.get_file_contents(key) </span></div><div><span style="font-family: Courier; "> return body</span></div><div><span style="font-family: Courier; "><br></span></div><div><span style="font-family: Courier; "> def get_file_contents(self, fname):</span></div><div><span style="font-family: Courier; "> print 'get_file_contents'</span></div><div><span style="font-family: Courier; "><br></span></div><div><span style="font-family: Courier; "> with open(fname) as f:</span></div><div><span style="font-family: Courier; "> return f.read()</span></div><div><span style="font-family: Courier; "><br></span></div><div><span style="font-family: Courier; "> def put_message(self, key, message):</span></div><div><span style="font-family: Courier; "> print 'put_message'</span></div><div><span style="font-family: Courier; "><br></span></div><div><span style="font-family: Courier; "> self._channel.basic_publish(</span></div><div><span style="font-family: Courier; "> exchange='my_log_exchange',</span></div><div><span style="font-family: Courier; "> routing_key=key,</span></div><div><span style="font-family: Courier; "> body=json.dumps(message),</span></div><div><span style="font-family: Courier; "> properties=self._properties</span></div><div><span style="font-family: Courier; "> )</span></div><div><span style="font-family: Courier; "><br></span></div><div><span style="font-family: Courier; "> def channel_on_open_callback(self, channel):</span></div><div><span style="font-family: Courier; "> print 'channel_on_open_callback: channel:{0}'.format(channel)</span></div><div><span style="font-family: Courier; "> self._channel = channel</span></div><div><span style="font-family: Courier; "> self.put_log() </span></div><div><span style="font-family: Courier; "> </span></div><div><span style="font-family: Courier; "> def connection_callback(self, connection):</span></div><div><span style="font-family: Courier; "> print 'connection_callback'</span></div><div><span style="font-family: Courier; "> 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; "> def connection(self):</span></div><div><span style="font-family: Courier; "> print 'connection'</span></div><div><span style="font-family: Courier; "> parameters = pika.ConnectionParameters(</span></div><div><span style="font-family: Courier; "> virtual_host='/',</span></div><div><span style="font-family: Courier; "> credentials=self._credentials</span></div><div><span style="font-family: Courier; "> )</span></div><div><span style="font-family: Courier; "> </span></div><div><span style="font-family: Courier; "> self._connection = pika.SelectConnection(parameters, self.connection_callback)</span></div><div><span style="font-family: Courier; "> </span></div><div><span style="font-family: Courier; "> def connection_close(self):</span></div><div><span style="font-family: Courier; "> self._connection.close()</span></div><div><span style="font-family: Courier; "> </span></div><div><span style="font-family: Courier; "> def stop(self): # called on interrupt</span></div><div><span style="font-family: Courier; "> self.connection_close()</span></div><div><span style="font-family: Courier; "> 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; "> def run(self):</span></div><div><span style="font-family: Courier; "> print 'run'</span></div><div><span style="font-family: Courier; "> self.connection()</span></div><div><span style="font-family: Courier; "> self._connection.ioloop.start()</span></div><div><span style="font-family: Courier; "> </span></div><div><span style="font-family: Courier; ">def main(argv=None):</span></div><div><span style="font-family: Courier; "> if argv is None: argv = sys.argv</span></div><div><span style="font-family: Courier; "> basic_publish = BasicPublish()</span></div><div><span style="font-family: Courier; "> </span></div><div><span style="font-family: Courier; "> try:</span></div><div><span style="font-family: Courier; "> basic_publish.run()</span></div><div><span style="font-family: Courier; "> except KeyboardInterrupt:</span></div><div><span style="font-family: Courier; "> print >>sys.stderr, "Interrupt!"</span></div><div><span style="font-family: Courier; "> basic_publish.stop()</span></div><div><span style="font-family: Courier; "><br></span></div><div><span style="font-family: Courier; ">if __name__ == "__main__":</span></div><div><span style="font-family: Courier; "> 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 <<a href="mailto:jerryk@rbcon.com">jerryk@rbcon.com</a>><br><span style="font-weight:bold">Reply-To: </span> rabbitmq <<a href="mailto:rabbitmq-discuss@lists.rabbitmq.com">rabbitmq-discuss@lists.rabbitmq.com</a>><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 <<a href="mailto:rabbitmq-discuss@lists.rabbitmq.com">rabbitmq-discuss@lists.rabbitmq.com</a>><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: The Erlang client is quite reliable and, indeed, is used by us internally for a range of things. 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>+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"><<a href="mailto:videlalvaro@gmail.com" target="_blank">videlalvaro@gmail.com</a>></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 <<a href="mailto:mailinglists@energiequant.de">mailinglists@energiequant.de</a>> wrote:<br><br>
> Hi!<br>
><br>
> I wanted to use RabbitMQ for simple log collection but for some reason I<br>
> can't find any useful AMQP client library (and what good is a server<br>
> without a working client?). I'd like to implement in either Python or<br>
> Erlang but I'm unable to get a reliable connection - either connection<br>
> setup fails, strange error messages appear after a short uptime or the<br>
> client just cannot handle connection loss.<br>
><br>
> What I've written so far is a simple Python script that takes lines from<br>
> stdin, builds a dictionary from it and tries to send it via amqplib,<br>
> Pika or Kombu (which seems to decide on using amqplib internally). It's<br>
> nothing special, I already have a serialized string, that's all I need<br>
> to send.<br>
><br>
> However, Pika or amqplib (it's been a while since I tried them, so I<br>
> don't remember which one it was) appeared to have some buffer issue -<br>
> although the connection (to localhost) was still alive, I would get<br>
> error messages about messages queueing up in the client after a few<br>
> minutes. The other library would loose connection a few seconds after<br>
> connecting but doesn't notice. Kombu terminates when the connection to<br>
> RabbitMQ is lost, although I would expect Kombu to retry since I set<br>
> retry = True when calling publish on the Producer object. I'm already<br>
> running a reconnect in a loop with a try/except inside but my script<br>
> just terminates. Strange...<br>
><br>
> I also tried to use amqp_client in Erlang but failed to get any<br>
> connection (I always get unknown_host unless I enter nothing as host<br>
> name). I should add that I only read into Erlang theoretically so far<br>
> and didn't get to write much but example programs yet, so it's likely I<br>
> do something wrong here (yes, I already tried both binary lists and<br>
> regular lists, no difference; I even tried supplying IP addresses in<br>
> various forms - without success). Documentation is scarce and what I<br>
> found suggests that I'm doing nothing wrong. I would prefer staying with<br>
> Python for this implementatin anyway, just thought the client that comes<br>
> with RabbitMQ should be able to work fine - unfortunately I seem to be<br>
> wrong here. :(<br>
><br>
> My question is: Does anyone have a complete working example of a<br>
> *reliable* client in Python (or Erlang)? Maybe I'm too stupid to read<br>
> the (very brief?) documentation correctly or the libraries I tried so<br>
> far are just too buggy... What I started with was an example from<br>
> "RabbitMQ in Action" but even that didn't work as expected.<br>
><br>
> If it wasn't for getting logs piped in on the console I would try Java<br>
> clients instead as I expect them to be more mature - to me, the Python<br>
> libraries appear to be a bit messy, pardon my (maybe wrong) impression<br>
> but I'm starting to get very frustrated with this: I've never had more<br>
> problems to just get a simple plain string handed over to some other<br>
> system. All in all I've spent too many hours in trying to get anything<br>
> working and nothing has worked so far.<br>
><br>
> Thanks,<br>
> Daniel<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>
_______________________________________________<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>