Unable to connect to atlas from Pymongo

I have been able to connect to my atlas cluster from mongodb docker container using the below connection string:

docker container run --name mongo-mflix -it --rm mongo mongo --host mongodb+srv://mflix-jprgs.mongodb.net/test -u subhayan -p --authenticationDatabase admin --ssl

But using a simple Python script i am not able to:

import pymongo

uri = "mongodb+srv://subhayan:<password>@mflix-jprgs.mongodb.net"

client = pymongo.MongoClient(uri, connectTimeoutMS=200, retryWrites=True)

print(client.stats)

db = client.sample_mflix
print(db.list_collection_names())

I keep getting the below error message:

(mflix-python) ~/Desktop/Mongodb/M22/mflix-python:$ python pymongo_dummy.py 
Database(MongoClient(host=['mflix-shard-00-01-jprgs.mongodb.net:27017', 'mflix-shard-00-00-jprgs.mongodb.net:27017', 'mflix-shard-00-02-jprgs.mongodb.net:27017'], document_class=dict, tz_aware=False, connect=True, authsource='admin', replicaset='mflix-shard-0', ssl=True, connecttimeoutms=200, retrywrites=True), 'stats')
Traceback (most recent call last):
  File "pymongo_dummy.py", line 10, in <module>
    print(db.list_collection_names())
  File "/Users/subhayanbhattacharya/.local/share/virtualenvs/mflix-python-bG-GbFs8/lib/python3.7/site-packages/pymongo/database.py", line 856, in list_collection_names
    for result in self.list_collections(session=session, **kwargs)]
  File "/Users/subhayanbhattacharya/.local/share/virtualenvs/mflix-python-bG-GbFs8/lib/python3.7/site-packages/pymongo/database.py", line 819, in list_collections
    _cmd, read_pref, session)
  File "/Users/subhayanbhattacharya/.local/share/virtualenvs/mflix-python-bG-GbFs8/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1455, in _retryable_read
    read_pref, session, address=address)
  File "/Users/subhayanbhattacharya/.local/share/virtualenvs/mflix-python-bG-GbFs8/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1254, in _select_server
    server = topology.select_server(server_selector)
  File "/Users/subhayanbhattacharya/.local/share/virtualenvs/mflix-python-bG-GbFs8/lib/python3.7/site-packages/pymongo/topology.py", line 231, in select_server
    address))
  File "/Users/subhayanbhattacharya/.local/share/virtualenvs/mflix-python-bG-GbFs8/lib/python3.7/site-packages/pymongo/topology.py", line 189, in select_servers
    selector, server_timeout, address)
  File "/Users/subhayanbhattacharya/.local/share/virtualenvs/mflix-python-bG-GbFs8/lib/python3.7/site-packages/pymongo/topology.py", line 205, in _select_servers_loop
    self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: SSL handshake failed: mflix-shard-00-01-jprgs.mongodb.net:27017: _ssl.c:1039: The handshake operation timed out,SSL handshake failed: mflix-shard-00-00-jprgs.mongodb.net:27017: _ssl.c:1039: The handshake operation timed out,SSL handshake failed: mflix-shard-00-02-jprgs.mongodb.net:27017: _ssl.c:1039: The handshake operation timed out

You have to replace <password> with your password.

1 Like