DAS_2024_1/artamonova_tatyana_lab_4/Publisher.py

32 lines
822 B
Python
Raw Normal View History

2024-11-04 01:31:41 +04:00
import pika
import json
import time
import random
credentials = pika.PlainCredentials('guest', 'guest')
connection = pika.BlockingConnection(
pika.ConnectionParameters(host='localhost', credentials=credentials))
channel = connection.channel()
channel.exchange_declare(exchange='order_events', exchange_type='fanout')
while True:
event = {
'event_type': 'order_created',
'order_id': random.randint(1000, 9999),
'customer_name': f'Клиент {random.randint(1, 100)}',
'product_name': f'Товар {random.randint(1, 10)}',
'quantity': random.randint(1, 10),
'timestamp': time.time()
}
channel.basic_publish(
exchange='order_events',
routing_key='',
body=json.dumps(event)
)
print(f'Опубликовано событие: {event}')
time.sleep(1)
connection.close()