Updated on 2025-12-22 GMT+08:00

Configuring Clients in Python

This section describes how to access a RabbitMQ instance using a RabbitMQ client in Python on the Linux CentOS, including how to install the client, and produce and consume messages.

Before getting started, ensure that you have collected the information described in Collecting Connection Information.

The connection examples in this document are applicable for both RabbitMQ 3.x.x and AMQP-0-9-1.

Preparing the Environment

  • Python

    Generally, Python is pre-installed in the system. You can enter python or python3 in the command line to check whether Python has been installed. The python command checks whether python 2.x has been installed and the python3 command checks whether python 3.x has been installed. Try both when you are not sure of the version.

    For example, if the following information is displayed, Python 3.x has been installed:

    [root@ecs-test python]# python3
    Python 3.7.1 (default, Jul  5 2020, 14:37:24) 
    [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>>

    If Python is not installed, run the following command to install it:

    yum install python

  • A RabbitMQ client in Python. In this document, pika is used as an example.

    Run the following command to install the recommended version of pika:

    pip install pika

    If pika cannot be installed using the pip command, run the following pip3 command instead:

    pip3 install pika

Producing Messages

Replace the information in bold with the actual values.

  • SSL authentication
    import pika
    import ssl
    
    #Connection information
    conf = {
        'host': 'ip',
        'port': 5671,
        'queue_name': 'queue-test',
        'username': 'root',
        'password': 'password'
    }
    
    context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
    credentials = pika.PlainCredentials(conf['username'], conf['password'])
    parameters = pika.ConnectionParameters(conf['host'],
                                           conf['port'],
                                           '/',
                                           credentials,
                                           ssl_options=pika.SSLOptions(context))
    
    connection = pika.BlockingConnection(parameters)
    channel = connection.channel()
    channel.queue_declare(conf['queue_name'])
    data = bytes('Hello World!', encoding='utf-8')
    channel.basic_publish(exchange='',
                          routing_key=conf['queue_name'],
                          body=data)
    
    print(" [x] Sent 'Hello World!'")
    
    connection.close()
  • Non-SSL authentication
    import pika
    
    #Connection information
    conf = {
        'host': 'ip',
        'port': 5672,
        'queue_name': 'queue-test',
        'username': 'root',
        'password': 'password'
    }
    
    credentials = pika.PlainCredentials(conf['username'], conf['password'])
    parameters = pika.ConnectionParameters(conf['host'],
                                           conf['port'],
                                           '/',
                                           credentials)
    
    connection = pika.BlockingConnection(parameters)
    channel = connection.channel()
    
    channel.queue_declare(conf['queue_name'])
    
    data = bytes("Hello World!", encoding="utf-8")
    
    channel.basic_publish(exchange='',
                          routing_key=conf['queue_name'],
                          body=data)
    
    print(" [x] Sent 'Hello World!'")
    
    connection.close()

Consuming Messages

Replace the information in bold with the actual values.

  • SSL authentication
    import pika
    import ssl
    
    #Connection information
    conf = {
        'host': 'ip',
        'port': 5671,
        'queue_name': 'queue-test',
        'username': 'root',
        'password': 'password'
    }
    
    context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
    credentials = pika.PlainCredentials(conf['username'], conf['password'])
    parameters = pika.ConnectionParameters(conf['host'],
                                           conf['port'],
                                           '/',
                                           credentials,
                                           ssl_options=pika.SSLOptions(context))
    
    connection = pika.BlockingConnection(parameters)
    channel = connection.channel()
    channel.queue_declare(conf['queue_name'])
    
    
    def callback(ch, method, properties, body):
        print(" [x] Received %r" % body.decode('utf-8'))
    
    
    channel.basic_consume(queue=conf['queue_name'], on_message_callback=callback, auto_ack=True)
    
    print(' [*] Waiting for messages. To exit press CTRL+C')
    channel.start_consuming()
  • Non-SSL authentication
    import pika
    
    #Connection information
    conf = {
        'host': 'ip',
        'port': 5672,
        'queue_name': 'queue-test',
        'username': 'root',
        'password': 'password'
    }
    
    credentials = pika.PlainCredentials(conf['username'], conf['password'])
    parameters = pika.ConnectionParameters(conf['host'],
                                           conf['port'],
                                           '/',
                                           credentials)
    
    connection = pika.BlockingConnection(parameters)
    channel = connection.channel()
    channel.queue_declare(conf['queue_name'])
    
    
    def callback(ch, method, properties, body):
        print(" [x] Received %r" % body.decode('utf-8'))
    
    
    channel.basic_consume(queue=conf['queue_name'], on_message_callback=callback, auto_ack=True)
    
    print(' [*] Waiting for messages. To exit press CTRL+C')
    channel.start_consuming()