Pymongo ServerSelectionTimeoutError randomly occuring. [MONGODB]

I’ve tried googling this for ages and just can’t seem to find a solution. I’m using MongoDB atlas, but keep randomly getting this error:

pymongo.errors.ServerSelectionTimeoutError: ac-h4o62zv-shard-00-01.xrot0sj.mongodb.net:27017: [Errno -3] Temporary failure in name resolution,ac-h4o62zv-shard-00-02.xrot0sj.mongodb.net:27017: [Errno -3] Temporary failure in name resolution,ac-h4o62zv-shard-00-00.xrot0sj.mongodb.net:27017: [Errno -3] Temporary failure in name resolution, Timeout: 30s, Topology Description: <TopologyDescription id: 63c589aec4187723b4ae35fe, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('ac-h4o62zv-shard-00-00.xrot0sj.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('ac-h4o62zv-shard-00-00.xrot0sj.mongodb.net:27017: [Errno -3] Temporary failure in name resolution')>, <ServerDescription ('ac-h4o62zv-shard-00-01.xrot0sj.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('ac-h4o62zv-shard-00-01.xrot0sj.mongodb.net:27017: [Errno -3] Temporary failure in name resolution')>, <ServerDescription ('ac-h4o62zv-shard-00-02.xrot0sj.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('ac-h4o62zv-shard-00-02.xrot0sj.mongodb.net:27017: [Errno -3] Temporary failure in name resolution')>]>

I can change nothing in my code and it will go away after a few hours of not working. Then everything will be find for a few days and suddenly this again.

Does anyone know what’s causing this and what I can do to fix it?

*Connection Code: *

import pymongo, dns, os, hashlib
from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi

client = MongoClient(os.environ["MONGO"], server_api=ServerApi('1'))

db = ["dbname"]["collection"]

# code will work fine till here:

users = db.find({})

print(list(users))

The error seems to happen as soon as I try an insert, find or replace

I have the same issue please help