{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# AMQP / RabbitMQ" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": true }, "outputs": [], "source": [ "parameters = pika.URLParameters('amqp://guest:guest@172.23.99.195:5672/%2F')\n", "connection = pika.BlockingConnection(parameters)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import pika\n", "\n", "connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))\n", "channel = connection.channel()\n", "channel.queue_declare(queue='hello')\n", "\n", "channel.basic_publish(exchange='', routing_key='hello', body='World1!')\n", "channel.basic_publish(exchange='', routing_key='hello', body='World2b!')\n", "channel.basic_publish(exchange='', routing_key='hello', body='World3b!')\n", "connection.close()" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "```\n", "docker exec rabbitmq rabbitmqctl list_queues\n", "```\n", "Listing queues ...\n", "hello\t1" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " [x] Sent '-f':'/root/.local/share/jupyter/runtime/kernel-39351ee5-5352-4665-9bc3-db9075c95ad4.json'\n" ] } ], "source": [ "# https://www.rabbitmq.com/tutorials/tutorial-four-python.html\n", "\n", "import pika\n", "import sys\n", "\n", "connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))\n", "channel = connection.channel()\n", "\n", "channel.exchange_declare(exchange='direct_logs',\n", " type='direct')\n", "\n", "severity = sys.argv[1] if len(sys.argv) > 2 else 'info'\n", "message = ' '.join(sys.argv[2:]) or 'Hello World!'\n", "channel.basic_publish(exchange='direct_logs',\n", " routing_key=severity,\n", " body=message)\n", "print(\" [x] Sent %r:%r\" % (severity, message))\n", "connection.close()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.1" } }, "nbformat": 4, "nbformat_minor": 2 }