Updated on 2023-09-13 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.

Preparing the Environment

  • Python

    Generally, Python is pre-installed in the system. Enter python in a CLI. If the following information is displayed, Python has already 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()