import json import pika HOST = ‘localhost’ QUEUENAME = ‘dataqueue’ def…
- User
import json import pika
HOST = ‘localhost’ QUEUE_NAME = ‘data_queue’
def handle_message(channel, method, properties, body): data = json.loads(body.decode(‘utf-8’)) print(f’Received: {data[“A”]}‘) response = {‘message’: f’Processed: {data[“A”]}’} channel.basic_publish( exchange=‘’, routing_key=properties.reply_to, properties=pika.BasicProperties(correlation_id=properties.correlation_id), body=json.dumps(response) )
def main(): connection = pika.BlockingConnection(pika.ConnectionParameters(host=HOST)) channel = connection.channel() channel.queue_declare(queue=QUEUE_NAME)
channel.basic_consume(queue=QUEUE_NAME, on_message_callback=handle_message, auto_ack=True) print(‘Server running…’) channel.start_consuming()if name == ‘main’: main()
Traceback (most recent call last): File “K:\Files\sc_temp\serverRabbitMQ.py”, line 29, in main() File “K:\Files\sc_temp\serverRabbitMQ.py”, line 19, in main connection = pika.BlockingConnection(pika.ConnectionParameters(host=HOST)) File “C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\pika\adapters\blocking_connection.py”, line 360, in init self._impl = self._create_connection(parameters, _impl_class) File “C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\pika\adapters\blocking_connection.py”, line 451, in _create_connection raise self._reap_last_connection_workflow_error(error) pika.exceptions.AMQPConnectionError
- Assistant
The error message suggests that there is an AMQP (Advanced Message Queuing Protocol) connection error. This error occurs when the connection to the RabbitMQ server cannot be established.
To resolve this issue, you can try the following steps:
-
Make sure that you have RabbitMQ server installed and running on the specified host (localhost). You can verify the RabbitMQ server status by accessing the RabbitMQ management UI or using the command-line tool.
-
Check if the host and port specified in the
pika.BlockingConnectionparameters are correct. The default RabbitMQ server host is 'localhost' and the default port is 5672. If you have a different host or port, make sure to update theHOSTvariable accordingly. -
Verify that you have the pika library installed. You can install it using pip:
pip install pika. -
Check for any network connectivity issues that might prevent the client from connecting to the RabbitMQ server. Ensure that there are no firewalls or network configurations blocking the communication.
-