aboutsummaryrefslogtreecommitdiffstats
path: root/rmq/publisher.py
diff options
context:
space:
mode:
authorGravatar Christopher Baklid <[email protected]>2018-05-23 22:38:20 +0200
committerGravatar Christopher Baklid <[email protected]>2018-05-23 22:38:20 +0200
commit309a6f93f878fc96951902fc47d45a30ef5f8d71 (patch)
treef788b43a892a93d0f97da73f459a55b43e1ea1a0 /rmq/publisher.py
parentupdate readme (diff)
POC completed
Diffstat (limited to 'rmq/publisher.py')
-rw-r--r--rmq/publisher.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/rmq/publisher.py b/rmq/publisher.py
new file mode 100644
index 0000000..4ba9db9
--- /dev/null
+++ b/rmq/publisher.py
@@ -0,0 +1,26 @@
+import pika
+
+def publish(message, username='guest', password='guest', host='localhost', port=5672, queue='', routingkey='', exchange='', exchange_type=''):
+ credentials = pika.PlainCredentials(username, password)
+ connection = pika.BlockingConnection(pika.ConnectionParameters(host, port, '/', credentials))
+ properties = pika.BasicProperties(content_type='text/plain', delivery_mode=1)
+
+ channel = connection.channel()
+ channel.queue_declare(queue=queue, durable=False)
+ channel.exchange_declare(exchange=exchange, exchange_type=exchange_type)
+ channel.queue_bind(exchange=exchange, queue=queue, routing_key=routingkey)
+
+ result = channel.basic_publish(
+ exchange=exchange,
+ routing_key=routingkey,
+ body=message,
+ properties=properties
+ )
+
+ if result:
+ print(f"Connecting to host: {host} port: {port} exchange: {exchange} queue: {queue}", flush=True)
+ else:
+ print("not delivered")
+
+ connection.close()
+