MongoDB not connecting with pymongo giving this error: [Errno 54] Connection reset by peer

This is what I am using to connect to MongoDB. I had previously had tls = false and once I removed it I can now connect locally to the mongo server. However I still get an error when it tries to connect with pythonanywhere. This is the code I use to connect to mongo.

def connect_db():
    if client is None:  
        print("Setting client because it is None.")
        if os.environ.get("CLOUD_MONGO", LOCAL) == CLOUD:
            password = os.environ.get("MONGODB_PASSWORD")
            if not password:
                raise ValueError('You must set your password '
                                 + 'to use Mongo in the cloud.')
            print("Connecting to Mongo in the cloud.")
            client = pm.MongoClient(f'mongodb+srv://{user}:{password}'
                                    + '@{databasename}.{}.mongodb.net'
                                    + '/?retryWrites=true&w='
                                    + 'majority')
        else:
            print("Connecting to Mongo locally.")
            client = pm.MongoClient()

And I get this error when running in pythonanywhere:

[SSL: TLSV1_ALERT_INTERNAL_ERROR] tlsv1 alert internal error (_ssl.c:997)

(configured timeouts: connectTimeoutMS: 20000.0ms)

If you have any ideas of suggestions I would appreciate it.