Skip to content

Instantly share code, notes, and snippets.

@ryanermita
Last active July 25, 2019 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanermita/82587ab703c2f0413827aa7f673a5f7a to your computer and use it in GitHub Desktop.
Save ryanermita/82587ab703c2f0413827aa7f673a5f7a to your computer and use it in GitHub Desktop.
sample publisher using rabbitmq pika client
import pika
import sys
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='direct_exchange', exchange_type='direct')
channel.queue_declare(queue='direct_queue', durable=True)
channel.queue_bind(exchange='direct_exchange', queue="direct_queue", routing_key="direct.routing.key")
message = " ".join(sys.argv[1:]) or "Hello World!"
channel.confirm_delivery()
try:
channel.basic_publish(exchange='direct_exchange', routing_key='direct.routing.key',
body=message, properties=pika.BasicProperties(delivery_mode=2)
)
print("Sent %r" % message)
except pika.exceptions.UnroutableError:
print("Failed to send message %r" % message)
connection.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment