Question

In Python 3: Now, you will need two Python source files. One sender (send.py) and one...

In Python 3: 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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the code for the given problem:-

SENDER.py

# doing using the RabbitMQ Libraries import pika import time # making the connection with the localhost connection = pika.Blo

CODE:-

# doing using the RabbitMQ libraries

import pika

import time

# making the connection with the localhost

connection = pika.BlockingConnection(

    pika.ConnectionParameters(host='localhost'))

# define the channel of connection

channel = connection.channel()

# declare the queue

channel.queue_declare(queue='test')

# using the for loop to send 0 to 9 number

for i in range(10):

    # publish the message

    channel.basic_publish(exchange='', routing_key='test', body='My name is John Doe and I love Python! '+ str(i))

    print(" Sent My name is John Doe and I love Python! ",i)

    # wait for 1 second

    time.sleep(1)

# close the connection

connection.close()

RECEIVER.py

# doing using the RabbitMQ Libraries import pika # making the connection with the localhost connection = pika.BlockingConnect

CODE:-

# doing using the RabbitMQ libraries

import pika

# making the connection with the localhost

connection = pika.BlockingConnection(

    pika.ConnectionParameters(host='localhost'))

# define the channel of connection

channel = connection.channel()

# declare the queue

channel.queue_declare(queue='test')

# callback function is called when the receiver receives something

def callback(ch, method, properties, body):

    print("%r" % body)

# define the consumer

channel.basic_consume(

    queue='test', on_message_callback=callback, auto_ack=True)

print('Waiting for messages. To exit press CTRL+C')

# start receiving messages from sender

channel.start_consuming()

SAMPLE OUTPUT:-

sender

Sent My name is John Doe and I love Python! Sent My name is John Doe and I love Python! 1, Sent My name is John Doe and I lov

receiver

] Waiting for messages. To exit press CTRL+C My name is John Doe and I love Python! O My name is John Doe and I love Python

NOTE:- I have added comments in the code for better understanding.

ALL THE BEST!

Add a comment
Know the answer?
Add Answer to:
In Python 3: Now, you will need two Python source files. One sender (send.py) and one...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • MESSAGE QUEUES WITH RABBITMQ

    In this assignment you will need to install Erlang (www.erlang.org/downloads) and the RabbitMQ server (https://www.rabbitmq.com/download.html) for your operating system. Finally, for protocol support you will need to install the Pika module. From PyCharm inside your project go to File > Settings > Project Settings > Project Interpreter. From there click on the green “+” to add a package to the project. Enter Pika in the search field at the top and select Add Package. Now, you will need two Python source...

  • In C++ Task 3: Use the stack and queue to simulate receiving and transforming data We are creating a system that will co...

    In C++ Task 3: Use the stack and queue to simulate receiving and transforming data We are creating a system that will convert strings sent over a serial bus one character at a time. The conversion will be from big to little endian or from little to big endian. To simplify this, each character will be considered a word. Little endian will have the lowest address first. Big endian will have the biggest address first. For example (for this lab),...

  • This is for a Unix class. Please help me out. I am attaching a skeletal code of the program below, it just needs ti be filled in. Below is a skeletal code of the program. Fork a child process...

    This is for a Unix class. Please help me out. I am attaching a skeletal code of the program below, it just needs ti be filled in. Below is a skeletal code of the program. Fork a child process and then use the parent for reading and the child for writing.  This is just a way of sending and receiving messages asynchronously. /* ************************************************************* * Utility functions * ************************************************************** */ static void usageError(const char * progName, const char *msg) {...

  • Modify the programs so that each program can both receive and send messages alternatively. Note that when you run the two programs, you should run them in two different windows ( terminals). Y...

    Modify the programs so that each program can both receive and send messages alternatively. Note that when you run the two programs, you should run them in two different windows ( terminals). You should be able to send messages from one to the other and terminate them by entering "end" //msgl.cpp / Here's the receiver program. / #include #include #include #1nclude #include <stdlib.h> <stdio.h> <string.h> <errno.h> <unistd.h> #include <sys/types.h> #include <sys/ipc.h> Winclude <sys/msg.h> struct my msg st f long int...

  • Here is the description of the client and server programs that you need to develop in C using TCP: Suppose we have a simple student query system, where the server keeps student's info in an array...

    Here is the description of the client and server programs that you need to develop in C using TCP: Suppose we have a simple student query system, where the server keeps student's info in an array of struct student_info ( char abc123171 char name [101 double GPA; To simplify the tasks, the server should create a static array of 10 students with random abc123, name, and GPA in the server program. Then the server waits for clients. When a client...

  • I'm using Python 3.7 with Spyder I need the full code and the same output as...

    I'm using Python 3.7 with Spyder I need the full code and the same output as the sample above Resources file: https://drive.google.com/file/d/1e5a21ZKRj2H_jOnWvg7HcjUKjJlY84KE/view -   https://drive.google.com/file/d/1XIA41ra8AaKjFuxO5VpwVkn90bxwDyB5/view Task description Baye's Theorem can be used to build many machine learning applications, including spam classifier Spam Classifier in Python from scratch is a tutorial which explains how to use Bave's Theorem and Python to develop a spam classifier step by step To train the spam classifier, one dataset "spam.csv" is used in the program Its...

  • Python programming- Download the following two files you will need for this activity: customerData.csv This file...

    Python programming- Download the following two files you will need for this activity: customerData.csv This file contains randomly generated fictitious customer data. customer_regex.py This is a Python script that imports the customer data into a list of customer details. In your personal playground in Codio, upload the two files and investigate the contents before considering the task you will pose to your peers. Assume the position of a manager of an online retailer. Pose a question to your IT expert...

  • Using python 3 to create the UDP Ping Clien and server. Using UDP sockets, you will...

    Using python 3 to create the UDP Ping Clien and server. Using UDP sockets, you will write a client and server program that enables the client to determine the round-trip time (RTT) to the server. To determine the RTT delay, the client records the time on sending a ping request to the server, and then records the time on receiving a ping response from the server. The difference in the two times is the RTT. The ping message contains 2...

  • Piggy back on the programming project one you completed in module 3. You will use some...

    Piggy back on the programming project one you completed in module 3. You will use some of those concepts here again. We will be building this project inside out, start individual quiz and use a for loop to repeat it three times for three students. In this program you will be required to write a python program that generates math quizzes for students in second grade. Your program should do the following Ask the student for their name Provide 3...

  • Needs Help with Java programming language For this assignment, you need to write a simulation program...

    Needs Help with Java programming language For this assignment, you need to write a simulation program to determine the average waiting time at a grocery store checkout while varying the number of customers and the number of checkout lanes. Classes needed: SortedLinked List: Implement a generic sorted singly-linked list which contains all of the elements included in the unsorted linked list developed in class, but modifies it in the following way: • delete the addfirst, addlast, and add(index) methods and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT