How can I list or discover queues on a RabbitMQ exchange using python?
There does not seem to be a direct AMQP-way to manage the server but there is a way you can do it from Python. I would recommend using a subprocess module combined with the rabbitmqctl
command to check the status of the queues.
I am assuming that you are running this on Linux. From a command line, running:
rabbitmqctl list_queues
will result in:
Listing queues ...pings 0receptions 0shoveled 0test1 55199...done.
(well, it did in my case due to my specific queues)
In your code, use this code to get output of rabbitmqctl
:
import subprocessproc = subprocess.Popen("/usr/sbin/rabbitmqctl list_queues", shell=True, stdout=subprocess.PIPE)stdout_value = proc.communicate()[0]print stdout_value
Then, just come up with your own code to parse stdout_value
for your own use.
As far as I know, there isn't any way of doing this. That's nothing to do with Python, but because AMQP doesn't define any method of queue discovery.
In any case, in AMQP it's clients (consumers) that declare queues: publishers publish messages to an exchange with a routing key, and consumers determine which queues those routing keys go to. So it does not make sense to talk about queues in the absence of consumers.
You can add plugin rabbitmq_management
sudo /usr/lib/rabbitmq/bin/rabbitmq-plugins enable rabbitmq_managementsudo service rabbitmq-server restart
Then use rest-api
import requestsdef rest_queue_list(user='guest', password='guest', host='localhost', port=15672, virtual_host=None): url = 'http://%s:%s/api/queues/%s' % (host, port, virtual_host or '') response = requests.get(url, auth=(user, password)) queues = [q['name'] for q in response.json()] return queues
I'm using requests library in this example, but it is not significantly.
Also I found library that do it for us - pyrabbit
from pyrabbit.api import Clientcl = Client('localhost:15672', 'guest', 'guest')queues = [q['name'] for q in cl.get_queues()]