Ema Il
Ema Il

Reputation: 417

Can't connect to Cassandra using python but can through cqlsh

I'm trying to test my pod connection to Cassandra using python. This is the code that I have:

host = '<hostname>'
port_num = 9092
default_fetch_size = 5000
idle_heartbeat_interval = 30
idle_heartbeat_timeout = 30
connect_timeout = 10
executor_threads = 2
protocol_version = 4
db_keyspace = 'fraud'

auth_provider = PlainTextAuthProvider(username=username, password=password)
cluster = Cluster([host], port=port_num, auth_provider=auth_provider,
                        idle_heartbeat_interval=idle_heartbeat_interval,
                        idle_heartbeat_timeout=idle_heartbeat_timeout,
                        connect_timeout=connect_timeout,
                        protocol_version=protocol_version,
                        executor_threads=executor_threads)
session = cluster.connect()

And I get the error:

Traceback (most recent call last):
  File "run.py", line 3, in <module>
    CassProvider().test_cassandra()
  File "/app/tmp_cassandra_connection.py", line 59, in test_cassandra
    cassandra_connector = CassandraDbConnector().get_session()
  File "/usr/local/lib/python3.6/site-packages/project/src/objects/type_objects.py", line 11, in __call__
    cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
  File "/app/tmp_cassandra_connection.py", line 41, in __init__
    session = cluster.connect()
  File "cassandra/cluster.py", line 1664, in cassandra.cluster.Cluster.connect
  File "cassandra/cluster.py", line 1700, in cassandra.cluster.Cluster.connect
  File "cassandra/cluster.py", line 1687, in cassandra.cluster.Cluster.connect
  File "cassandra/cluster.py", line 3485, in cassandra.cluster.ControlConnection.connect
  File "cassandra/cluster.py", line 3530, in cassandra.cluster.ControlConnection._reconnect_internal
cassandra.cluster.NoHostAvailable: ('Unable to connect to any servers', {'<ip>:9092': OSError(None, "Tried connecting to [('<ip>', 9092)]. Last error: timed out")

but when I get on the same pod using bash I can connect to Cassandra using cqlsh without a problem using the same host, default port, username and password.

Do you know why the python code on the pod gives me problems but the cqlsh is working perfectly?

Thanks

Upvotes: 2

Views: 459

Answers (1)

Aaron
Aaron

Reputation: 57843

'Unable to connect to any servers', {'<ip>:9092': 

Try setting the port to 9042, which is the default.

port_num = 9042

Upvotes: 3

Related Questions