import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='orders', exchange_type='direct')
channel.basic_publish(exchange='orders', routing_key='new_order', body='Order #123')
print("Сообщение отправлено")
connection.close()
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='orders', exchange_type='direct')
result = channel.queue_declare(queue='', exclusive=True)
queue_name = result.method.queue
channel.queue_bind(exchange='orders', queue=queue_name, routing_key='new_order')
def callback(ch, method, properties, body):
print(f"Получено сообщение: {body}")
channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True)
print('Ожидание сообщений...')
channel.start_consuming()