Getting ssl error while connecting

I am using the python to connect with MongoDB Atlas with mongoengine. the codes are as below.

import datetime as dt

import mongoengine as me

import shoonya_details as kd

today = dt.date.today().strftime('%d-%m-%Y')

me.connect(
    alias="core", db="algo_trading",
    host=f"mongodb+srv://{kd.MONGO_ID}:{kd.MONGO_PWD}@akcluster0.lw3clrm.mongodb.net/algo_trading"
)


class AccessToken(me.Document):
    date = me.StringField(default=today, unique=True)
    access_token = me.StringField(unique=True, required=True)

    meta = {
        "db_alias": "core",
        "collection": "accesstoken",
        "indexes": ["date"]
    }

and I am getting error as below:

pymongo.errors.ServerSelectionTimeoutError: ac-sa1chw2-shard-00-01.lw3clrm.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997),ac-sa1chw2-shard-00-00.lw3clrm.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997),ac-sa1chw2-shard-00-02.lw3clrm.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997), Timeout: 30s, Topology Description: <TopologyDescription id: 63f49e3534051c1e57bb1c20, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('ac-sa1chw2-shard-00-00.lw3clrm.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('ac-sa1chw2-shard-00-00.lw3clrm.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997)')>, <ServerDescription ('ac-sa1chw2-shard-00-01.lw3clrm.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('ac-sa1chw2-shard-00-01.lw3clrm.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997)')>, <ServerDescription ('ac-sa1chw2-shard-00-02.lw3clrm.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('ac-sa1chw2-shard-00-02.lw3clrm.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997)')>]>

I am using the Mac M1 chip.

Traceback (most recent call last):
  File "/Users/atulkundaria/PycharmProjects/pythonProject/pythonProject/finvasia/finvasia_login.py", line 47, in <module>
    _getting_login(shoonya)
  File "/Users/atulkundaria/PycharmProjects/pythonProject/pythonProject/finvasia/finvasia_login.py", line 32, in _getting_login
    UserToken(date=today, user_token=ret["susertoken"]).save()
  File "/Users/atulkundaria/PycharmProjects/pythonProject/pythonProject/finvasia/venv/lib/python3.10/site-packages/mongoengine/document.py", line 417, in save
    _ = self._get_collection()
  File "/Users/atulkundaria/PycharmProjects/pythonProject/pythonProject/finvasia/venv/lib/python3.10/site-packages/mongoengine/document.py", line 231, in _get_collection
    if cls._meta.get("auto_create_index", True) and db.client.is_primary:
  File "/Users/atulkundaria/PycharmProjects/pythonProject/pythonProject/finvasia/venv/lib/python3.10/site-packages/pymongo/mongo_client.py", line 1115, in is_primary
    return self._server_property("is_writable")
  File "/Users/atulkundaria/PycharmProjects/pythonProject/pythonProject/finvasia/venv/lib/python3.10/site-packages/pymongo/mongo_client.py", line 881, in _server_property
    server = self._topology.select_server(writable_server_selector)
  File "/Users/atulkundaria/PycharmProjects/pythonProject/pythonProject/finvasia/venv/lib/python3.10/site-packages/pymongo/topology.py", line 272, in select_server
    server = self._select_server(selector, server_selection_timeout, address)
  File "/Users/atulkundaria/PycharmProjects/pythonProject/pythonProject/finvasia/venv/lib/python3.10/site-packages/pymongo/topology.py", line 261, in _select_server
    servers = self.select_servers(selector, server_selection_timeout, address)
  File "/Users/atulkundaria/PycharmProjects/pythonProject/pythonProject/finvasia/venv/lib/python3.10/site-packages/pymongo/topology.py", line 223, in select_servers
    server_descriptions = self._select_servers_loop(selector, server_timeout, address)
  File "/Users/atulkundaria/PycharmProjects/pythonProject/pythonProject/finvasia/venv/lib/python3.10/site-packages/pymongo/topology.py", line 238, in _select_servers_loop
    raise ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError: ac-sa1chw2-shard-00-01.lw3clrm.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997),ac-sa1chw2-shard-00-02.lw3clrm.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997),ac-sa1chw2-shard-00-00.lw3clrm.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997), Timeout: 30s, Topology Description: <TopologyDescription id: 63f4a22a63c014d86b971aa2, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('ac-sa1chw2-shard-00-00.lw3clrm.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('ac-sa1chw2-shard-00-00.lw3clrm.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997)')>, <ServerDescription ('ac-sa1chw2-shard-00-01.lw3clrm.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('ac-sa1chw2-shard-00-01.lw3clrm.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997)')>, <ServerDescription ('ac-sa1chw2-shard-00-02.lw3clrm.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('ac-sa1chw2-shard-00-02.lw3clrm.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997)')>]>

Usually this indicates to me either the ssl/tls library is out of date or the certificate store needs updating.

Can’t really help as my Mac knowledge is: 0

Try running this command to update Python 3.10’s bundled openssl certificates:

/Applications/Python\ 3.10/Install\ Certificates.command

If that doesn’t resolve your issue, then delete and recreate your venv:

cd /Users/atulkundaria/PycharmProjects/pythonProject/pythonProject/finvasia/
rm -rf venv/
python3.10 -m venv venv

For more options see TLS/SSL and PyMongo — PyMongo 4.3.3 documentation

1 Like

The server certificate is not trusted by your “client”. Here the client may mean the lib/framework you are using to connect to that server, or the OS built-in list, depending on from where the list of trusted root certs are retrieved.

You can try updating python cert lists as said in above comment, or check trusted list on your mac OS.