Commit fa0ac3f1 by 岳巧源

add rabbit_mq

parent 479e5c0c
Showing with 37 additions and 0 deletions
import pika
class Producer:
def __init__(self,
user="",
password="",
host="localhost",
port=5672,
queue_name="",
durable=True,
exchange=None,
routing_key=None,
):
credentials = pika.PlainCredentials(username=user, password=password)
self.conn = pika.BlockingConnection(pika.ConnectionParameters(
host=host,
port=port,
credentials=credentials
))
channel = self.conn.channel()
channel.queue_declare(queue=queue_name, durable=durable)
self.channel = channel
def push(self, message, exchange=None, routing_key=None):
if exchange is None or routing_key is None:
self.channel.basic_publish(exchange="", routing_key="", body=str.encode(message))
self.channel.basic_publish(exchange=exchange, routing_key=routing_key, body=str.encode(message))
def close(self):
try:
self.channel.close()
self.conn.close()
except Exception as e:
print(e)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment