SSL Handshake issue with Pymongo on Python3

Deployed an app that uses MongoDB. Issue with SSL Cert and used this stack overflow answer

How does certifi work with streamlit cloud?

Error Message:

2023-08-21 13:05:43.229 Uncaught app exception
Traceback (most recent call last):
  File "/home/adminuser/venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 552, in _run_script
    exec(code, module.__dict__)
  File "/mount/src/pms_chef_bot/pages/2_Login.py", line 43, in <module>
    for record in cursor:
  File "/home/adminuser/venv/lib/python3.9/site-packages/pymongo/cursor.py", line 1251, in next
    if len(self.__data) or self._refresh():
  File "/home/adminuser/venv/lib/python3.9/site-packages/pymongo/cursor.py", line 1142, in _refresh
    self.__session = self.__collection.database.client._ensure_session()
  File "/home/adminuser/venv/lib/python3.9/site-packages/pymongo/mongo_client.py", line 1758, in _ensure_session
    return self.__start_session(True, causal_consistency=False)
  File "/home/adminuser/venv/lib/python3.9/site-packages/pymongo/mongo_client.py", line 1703, in __start_session
    self._topology._check_implicit_session_support()
  File "/home/adminuser/venv/lib/python3.9/site-packages/pymongo/topology.py", line 538, in _check_implicit_session_support
    self._check_session_support()
  File "/home/adminuser/venv/lib/python3.9/site-packages/pymongo/topology.py", line 554, in _check_session_support
    self._select_servers_loop(
  File "/home/adminuser/venv/lib/python3.9/site-packages/pymongo/topology.py", line 238, in _select_servers_loop
    raise ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError: SSL handshake failed: ac-57cw5mr-shard-00-01.ouufw1l.mongodb.net:27017: [SSL: TLSV1_ALERT_INTERNAL_ERROR] tlsv1 alert internal error (_ssl.c:1129),SSL handshake failed: ac-57cw5mr-shard-00-02.ouufw1l.mongodb.net:27017: [SSL: TLSV1_ALERT_INTERNAL_ERROR] tlsv1 alert internal error (_ssl.c:1129),SSL handshake failed: ac-57cw5mr-shard-00-00.ouufw1l.mongodb.net:27017: [SSL: TLSV1_ALERT_INTERNAL_ERROR] tlsv1 alert internal error (_ssl.c:1129), Timeout: 30s, Topology Description: <TopologyDescription id: 64e3610893ffc59f52a1c615, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('ac-57cw5mr-shard-00-00.ouufw1l.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('SSL handshake failed: ac-57cw5mr-shard-00-00.ouufw1l.mongodb.net:27017: [SSL: TLSV1_ALERT_INTERNAL_ERROR] tlsv1 alert internal error (_ssl.c:1129)')>, <ServerDescription ('ac-57cw5mr-shard-00-01.ouufw1l.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('SSL handshake failed: ac-57cw5mr-shard-00-01.ouufw1l.mongodb.net:27017: [SSL: TLSV1_ALERT_INTERNAL_ERROR] tlsv1 alert internal error (_ssl.c:1129)')>, <ServerDescription ('ac-57cw5mr-shard-00-02.ouufw1l.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('SSL handshake failed: ac-57cw5mr-shard-00-02.ouufw1l.mongodb.net:27017: [SSL: TLSV1_ALERT_INTERNAL_ERROR] tlsv1 alert internal error (_ssl.c:1129)')>]>

Here is an example how to use MongoDB with Streamlit:

I also have the same isue. I have implemented everything according to the doc, still the error is popping up. App is running fine on my localhost but it is throwing error when I try to deploy it on streamlit cloud.
Please Help !!!

Thanks for sharing!
I don’t know if we have to use that exact same naming convention such as

[mongo]
host = "localhost"
port = 27017
username = "xxx"
password = "xxx"

And do we need to specify a host or port since the goal is to deploy in production?

I have it like

[mongodb]
mongo_username = "xxxxx"
mongo_password="xxxxx"

I also create my secrets file and also put the same in the app settings like:

username = quote_plus(st.secrets["mongodb"]["mongo_username"])
password = quote_plus(st.secrets["mongodb"]["mongo_pwd"])
db_name = st.secrets["mongodb"]["mongo_dbname"]

and then in my code establish connection lilke

uri = f"mongodb+srv://{username}:{password}@{db_name}.ouufw1l.mongodb.net/?retryWrites=true&w=majority"

client = MongoClient(uri, server_api=ServerApi('1'))

And that works fine for me locally! But not in Streamlit deployment :frowning:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.