STOMP - Python - SSL

Updated on 20 Sep 2023

I am using the Python stomp.py library to send a message.

  • I am referencing the SSL certificate and ca-certificate files that we created earlier on lines 10, 11.
  • I am using a slightly different technique for connection so that I can pass in the certificate files
  • this example also uses a different approach by having the user/pass in the rabbitmq.conf file, so I don’t need to specify it in the code.
#!/usr/bin/env python3

import stomp
import sys

if len(sys.argv) > 1:

    conn = stomp.Connection([('localhost', 61614)], vhost='demo_host')
    conn.set_ssl(for_hosts=[('localhost', 61614)], 
                 key_file='/etc/rabbitmq/ssl/server_test2_key.pem', 
                 cert_file='/etc/rabbitmq/ssl/server_test2_certificate.pem')
    
    #-----
    #-- I can have this set in the /etc/rabbitmq/rabbitmq.conf file.
    #--
    #--stomp.default_user = demo_user
    #--stomp.default_pass = demo_pass

    #--and I wont have to pass the user / pass credentials to the connect method
    #-----
    #conn.connect('demo_user', 'demo_pass', wait=True)
    conn.connect()
    conn.send(body=sys.argv[1], destination='/queue/q1')
    conn.disconnect()

Sending the message

I will also make this file executable, and to call this code, I simply do the following.

./send.py "hello world from Python - ssl"

Rabbitmqadmin

I can use rabbitmqadmin to check the message

rabbitmqadmin list vhosts

And to see the message, I can use the following command.

rabbitmqadmin get queue=q1 --vhost=demo_host -u guest -p guest

I am using guest username because I have assigned the appropriate privileges to that account.