Question
Written in Python 3: Can someone please help me come up with the receive.py code? Below are the instructions and the current send.py code I
Written in Python 3: Can someone please help me come up with the receive.py code? Below are the instructions and the current send.py code I have written. I need help with creating the recieve.py code and them working together to get the output I need in the instructions.
Now, you will need two Python source files. One sender (send.py) and one receiver (receive.py). The sender will need establish a connection, channel and declare a queue. It will then need to send a message to the message queue once per second. You may use a while loop and the time.sleep function for this. The message should be something unique, like My name is John Doe and I love Python! # where is # is a running counter so you can see the message number. Each of the receivers, will need to establish a connection to the same queue, register a call back, and start consuming any messages received. When a message is received it should be output to standard out.This assignment is not very realistic when implemented on a single computer but recognize that localhost need not be the address. The message queue could be located on one server with dozens of senders and receivers each located anywhere. As you are running your code, noticed that you can start the server. It will begin sending messages. Wait a few seconds and start the receiver. Notice that all messages are received even though some were sent before the receiver was started. This is one of the benefits of message queues, i.e. it maintains the message history and can deliver messages to clients even if they momentarily lose connectivity.
Below is my current code for send.py. I now need someone to help write the receive.py so they work together to get the above outcome. Please feel free to revise any of my send.py code as well because I am getting error message when trying to run it. Thanks in advance.
#!/usr/bin/env python import pika import sys import time # create pika connection connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost')) channel = connection.channel() # create queue channel.queue_declare(queue="task-queue", durable=True) # variable to count the number 1 = 1 # loop will run 5 times and add messages in queue while i <= 5: # adding counter number at end to identify message uniques. message = "My name is John Doe and I love Python! #" + str(i) #publish message on queue channel.basic_publish( excahnge='', routing_key='task_queue', body=message, properties=pika.BasicProperties(delivery_mode=2), # make message persistent )) #print sent message on console print("Message sent: %r" % message) 1 = 1 + 1 # increase counter time.sleep(1) # sleep for 1 second # close the connection connection.close()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started