cloudamqp / python-amqp-example

How to connect from Python to CloudAMQP
http://www.cloudamqp.com/
14 stars 13 forks source link

Is it possible to provide a python example using pika and SSL #5

Open drivard opened 4 years ago

drivard commented 4 years ago

Hi,

It would be really appreciated to get the example of a working connection using the amqps scheme. I am currently trying to achieve that using the pika library but I always get the error:

ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1076)

I cannot tell if it is related to the Comodo CA or the OS lacking the intermediate CA or any of the usual ssl issues, since I tried many combinaisons as well as downloading the intermediate CA cert from comodo to ensure that it is known by my OS.

The connection sample I used is:

import pika
import ssl

credentials = pika.credentials.PlainCredentials(username='****', password='*****')
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
context.verify_mode = ssl.CERT_OPTIONAL  # also tried CERT_REQUIRED
parameters = pika.ConnectionParameters(
    host='*****.****.cloudamqp.com',
    port=5671,
    virtual_host='my-vhost',
    credentials=credentials,
    ssl_options=pika.SSLOptions(context)
)
connection = pika.BlockingConnection(parameters)

I am using python from mac os catalina and I used brew to install python. Python 3.7.5 in an isolated virtualenv with only pika installed. pika==1.1.0

Thanks for your help.

Gaurav3khede commented 4 years ago

Hi, Did you able to solve this problem, I am also facing same. Thanks.

drivard commented 4 years ago

I recently used this and it worked, while testing connectivity to a new instance.

pika==0.13.0

send.py

import ssl

credentials = pika.credentials.PlainCredentials(username='********', password='********')

parameters = pika.ConnectionParameters(
    host='***********.rmq.cloudamqp.com',
    port=5671,
    virtual_host='********',
    credentials=credentials,
    ssl=True
)
connection = pika.BlockingConnection(parameters)

# Create the channel and the queue
channel = connection.channel()
channel.queue_declare(queue='hello', durable=True)

# publish the message
channel.basic_publish(exchange='',
                      routing_key='hello',
                      body='Hello World!')

print(" [x] Sent 'Hello World!'")
connection.close()

receive.py

import pika
import ssl

credentials = pika.credentials.PlainCredentials(username='********', password='********')
parameters = pika.ConnectionParameters(
    ssl=True,
    host='********.rmq.cloudamqp.com',
    port=5671,
    virtual_host='********',
    credentials=credentials
)
connection = pika.BlockingConnection(parameters)

# Create the channel and the queue
channel = connection.channel()
channel.queue_declare(queue='hello', durable=True)

# consume the queue
def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)

channel.basic_consume(callback,
                      queue='hello',
                      no_ack=True)

print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
Praveenk8051 commented 3 years ago

@drivard thank you

johnfaucette commented 1 year ago

FYI: ssl=True does not work with version 1.3.2 of pika. I get TypeError: unexpected kwargs: {'ssl': True}. Currently, haven't found a way to get this working.

johanrhodin commented 1 year ago

@johnfaucette right, check this example that I'm trying to add to the Pika docs: https://github.com/pika/pika/pull/1436/commits/0975526996fcbd008c1ac39851b1801f73c4c3ca.

If you don't want to mess with "ssl.SSLContext" you can use URLParameters

johnfaucette commented 12 months ago

Thanks @johanrhodin . That example helps a lot 👍 .