#!/usr/bin/env python3 """ If script executed with arguments, all of them will be send over AMQP to reciver: $ python producer.py -n From: https://radeksm.github.io/2019/07/01/RabbitMQ_crash_course2.html """ import argparse import pika import os QNAME='queue0' RABBIT_PORT = 5672 RABBIT_IP = os.environ.get("RABBIT_IP", None) def message(seq_number=0): _msg = "AMQP MESSAGE {:d}".format(seq_number) return _msg def main(): if RABBIT_IP is None: print("Missing RABBIT_IP env variable") parser = argparse.ArgumentParser(description='Send some AMQP messages') parser.add_argument('-n', type=int, help="Number of messages to send") args = parser.parse_args() msg = message() print('AMQP producer connecting to {}:{} with message: \"{}\"'.\ format(RABBIT_IP, RABBIT_PORT, msg)) connection = pika.BlockingConnection(pika.ConnectionParameters(RABBIT_IP, RABBIT_PORT)) channel = connection.channel() channel.queue_declare(queue=QNAME) for x in range(0,args.n): channel.basic_publish(exchange='', routing_key=QNAME, body=message(x)) connection.close() if __name__ == '__main__': main()