How to pass along username and password to cassandra in python
Question: I'm learning and just setup my cassandra cluster and trying to use python as
the client to interact with it. In the yaml, I set the authenticator to be
PasswordAuthenticator.

So now I plan to provide my username and password over to the connect function
but find no where to put them.

    
    
    cluster = Cluster(hosts)
    session = cluster.connect(keyspace)
    

Basically, you provide only the host and the keyspace. The documentation kind
of suggest a connection with anonymous? <http://datastax.github.io/python-
driver/getting_started.html#connecting-to-cassandra>

If I just use the example, I will get the following error

    
    
    raise NoHostAvailable("Unable to connect to any servers", errors)
    cassandra.cluster.NoHostAvailable: ('Unable to connect to any servers', {'hou722067': AuthenticationFailed('Remote end requires authentication.',)})
    

Is the authentication information supplied through some config files? It seems
like a very basic functionality and I can't imagine it's not covered in the
python client driver. I must have miss something.

In short, my question is: How do I login to cassandra using python?

Thanks in advance for any hint possible!

================================================= Got it figured.

I should provide the username and password in the auth_provider field which is
a function returning a dictionary containing ‘username’ and ‘password’ keys
with appropriate string values.

Something like def getCredential(self, host): credential =
{'username':'myUser', 'password':'myPassword'} return credential

cluster = Cluster(nodes, auth_provider=getCredential)


Answer: Following the ops recommendation use:

    
    
    from cassandra.cluster import Cluster
    
    def getCredential(self):
        return {'username': 'foo', 'password': 'bar'} 
    
    node_ips = ['0.0.0.0', '0.0.0.1']
    
    cluster = Cluster(node_ips, protocol_version=1, auth_provider=getCredential)
    session = cluster.connect()
    

If you're using protocol version 2 then you must authenticate with an
AuthProvider object. EX:

    
    
    from cassandra.auth import PlainTextAuthProvider
    from cassandra.cluster import Cluster
    
    ap = PlainTextAuthProvider(username=foo, password=bar)
    c = Cluster(protocol_version=2, auth_provider=ap)
    s = c.connect()
    

Can someone can explain best practices using either of these methods to
authenticate with a remote cluster?

