diff --git a/rabbitmq/log.py b/rabbitmq/log.py new file mode 100644 index 0000000..6a2b458 --- /dev/null +++ b/rabbitmq/log.py @@ -0,0 +1,16 @@ +import sys + +import pika + +connection = pika.BlockingConnection( + pika.ConnectionParameters(host="localhost"), +) +channel = connection.channel() + +channel.exchange_declare(exchange="logs", exchange_type="fanout") + +message = " ".join(sys.argv[1:]) or "info: Hello World!" +channel.basic_publish(exchange="logs", routing_key="", body=message) +print(f" [x] Sent {message}") + +connection.close() diff --git a/rabbitmq/receive_log.py b/rabbitmq/receive_log.py new file mode 100644 index 0000000..ee127b0 --- /dev/null +++ b/rabbitmq/receive_log.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python +import os +import sys + +import pika + + +def main(): + connection = pika.BlockingConnection( + pika.ConnectionParameters(host="localhost"), + ) + channel = connection.channel() + + channel.exchange_declare(exchange="logs", exchange_type="fanout") + + result = channel.queue_declare(queue="", exclusive=True) + queue_name = result.method.queue + + channel.queue_bind(exchange="logs", queue=queue_name) + + def callback(ch, method, properties, body): + print(f" [x] {body.decode()}") + + print(" [*] Waiting for logs. To exit press CTRL+C") + channel.basic_consume( + queue=queue_name, + on_message_callback=callback, + auto_ack=True, + ) + + channel.start_consuming() + + +if __name__ == "__main__": + try: + main() + except KeyboardInterrupt: + print("Interrupted") + try: + sys.exit(0) + except SystemExit: + os._exit(0) \ No newline at end of file