Cassandra python client

A code example may be too much to ask for, but would be very helpful.

First, the simplest possible Cassandra client. No eventlet, no ssl. It works:

from cassandra.cluster import Cluster
cluster = Cluster(contact_points=['<ip>'],
                  connection_class=None)
session = cluster.connect('<keyspace>')
print("OK, session:", session)

Next, eventlet. No threads, so eventlet is pointless here. But it works:

from cassandra.cluster import Cluster
from cassandra.io.eventletreactor import EventletConnection
cluster = Cluster(contact_points=['<ip>'],
                  connection_class=EventletConnection)
session = cluster.connect('<keyspace>')
print("OK, session:", session)

Next, ssl, no eventlet. This also works:

import ssl
from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider
cluster = Cluster(contact_points=['<ip>'],
                  connection_class=None,
                  ssl_options=dict(ca_certs='<certfile>',
                                   cert_reqs=ssl.CERT_REQUIRED,
                                   ssl_version=ssl.PROTOCOL_TLSv1),
                  auth_provider=PlainTextAuthProvider(username='<user>',
                                                      password='<pass>'))
session = cluster.connect('<keyspace>')
print("OK, session:", session)

SSL with certfile and keyfile

import ssl
from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider
certfile = 'path/to/cert'
keyfile = 'path/to/key'
cluster = Cluster(contact_points=['<ip>'],
                  connection_class=None,
                  ssl_options=dict(ca_certs='<certfile>',
                                   certfile=certfile,
                                   keyfile=keyfile,
                                   cert_reqs=ssl.CERT_REQUIRED,
                                   ssl_version=ssl.PROTOCOL_TLSv1),
                  auth_provider=PlainTextAuthProvider(username='<user>',
                                                      password='<pass>'))
session = cluster.connect('<keyspace>')
print("OK, session:", session)

Finally, ssl and eventlet.

import ssl
from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider
from cassandra.io.eventletreactor import EventletConnection
cluster = Cluster(contact_points=['<ip>'],
                  connection_class=EventletConnection,
                  ssl_options=dict(ca_certs='<certfile>',
                                   cert_reqs=ssl.CERT_REQUIRED,
                                   ssl_version=ssl.PROTOCOL_TLSv1),
                  auth_provider=PlainTextAuthProvider(username='<user>',
                                                      password='<pass>'))
session = cluster.connect('<keyspace>')
print("OK, session:", session)

LEAVE A COMMENT