Flask Server Connection Issue - Resolution Lifetime Expired

Hello,

I’m encountering an issue while trying to launch my Flask server, specifically with the connection to MongoDB. I’m receiving the following error message:

Traceback (most recent call last):
  File "/Users/Jayson/opt/anaconda3/envs/colibriapi/lib/python3.10/site-packages/pymongo/srv_resolver.py", line 89, in _resolve_uri
    results = _resolve(
  File "/Users/Jayson/opt/anaconda3/envs/colibriapi/lib/python3.10/site-packages/pymongo/srv_resolver.py", line 43, in _resolve
    return resolver.resolve(*args, **kwargs)
  File "/Users/Jayson/opt/anaconda3/envs/colibriapi/lib/python3.10/site-packages/dns/resolver.py", line 1193, in resolve
    return get_default_resolver().resolve(qname, rdtype, rdclass, tcp, source,
  File "/Users/Jayson/opt/anaconda3/envs/colibriapi/lib/python3.10/site-packages/dns/resolver.py", line 1066, in resolve
    timeout = self._compute_timeout(start, lifetime,
  File "/Users/Jayson/opt/anaconda3/envs/colibriapi/lib/python3.10/site-packages/dns/resolver.py", line 879, in _compute_timeout
    raise LifetimeTimeout(timeout=duration, errors=errors)
dns.resolver.LifetimeTimeout: The resolution lifetime expired after 21.612 seconds: Server 132.167.198.4 UDP port 53 answered The DNS operation timed out.; Server 132.167.198.5 UDP port 53 answered The DNS operation timed out.; Server 132.167.198.4 UDP port 53 answered The DNS operation timed out.; Server 132.167.198.5 UDP port 53 answered The DNS operation timed out.; Server 132.167.198.4 UDP port 53 answered The DNS operation timed out.; Server 132.167.198.5 UDP port 53 answered The DNS operation timed out.; Server 132.167.198.4 UDP port 53 answered The DNS operation timed out.; Server 132.167.198.5 UDP port 53 answered The DNS operation timed out.; Server 132.167.198.4 UDP port 53 answered The DNS operation timed out.; Server 132.167.198.5 UDP port 53 answered The DNS operation timed out.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/Jayson/Documents/CEA/gitlab/Colibri/Colibri_v2/colibri_flask_api/app/server.py", line 1, in <module>
    from api_folder.controller.main import app
  File "/Users/Jayson/Documents/CEA/gitlab/Colibri/Colibri_v2/colibri_flask_api/app/api_folder/__init__.py", line 54, in <module>
    from api_folder.models.known_sources import *
  File "/Users/Jayson/Documents/CEA/gitlab/Colibri/Colibri_v2/colibri_flask_api/app/api_folder/models/known_sources.py", line 6, in <module>
    db_mongo = Get_MongoDB()
  File "/Users/Jayson/Documents/CEA/gitlab/Colibri/Colibri_v2/colibri_flask_api/app/api_folder/config/mongoDB.py", line 18, in Get_MongoDB
    client = pymongo.MongoClient(Params)
  File "/Users/Jayson/opt/anaconda3/envs/colibriapi/lib/python3.10/site-packages/pymongo/mongo_client.py", line 736, in __init__
    res = uri_parser.parse_uri(
  File "/Users/Jayson/opt/anaconda3/envs/colibriapi/lib/python3.10/site-packages/pymongo/uri_parser.py", line 542, in parse_uri
    nodes = dns_resolver.get_hosts()
  File "/Users/Jayson/opt/anaconda3/envs/colibriapi/lib/python3.10/site-packages/pymongo/srv_resolver.py", line 121, in get_hosts
    _, nodes = self._get_srv_response_and_hosts(True)
  File "/Users/Jayson/opt/anaconda3/envs/colibriapi/lib/python3.10/site-packages/pymongo/srv_resolver.py", line 101, in _get_srv_response_and_hosts
    results = self._resolve_uri(encapsulate_errors)
  File "/Users/Jayson/opt/anaconda3/envs/colibriapi/lib/python3.10/site-packages/pymongo/srv_resolver.py", line 97, in _resolve_uri
    raise ConfigurationError(str(exc))
pymongo.errors.ConfigurationError: The resolution lifetime expired after 21.612 seconds: Server 132.167.198.4 UDP port 53 answered The DNS operation timed out.; Server 132.167.198.5 UDP port 53 answered The DNS operation timed out.; Server 132.167.198.4 UDP port 53 answered The DNS operation timed out.; Server 132.167.198.5 UDP port 53 answered The DNS operation timed out.; Server 132.167.198.4 UDP port 53 answered The DNS operation timed out.; Server 132.167.198.5 UDP port 53 answered The DNS operation timed out.; Server 132.167.198.4 UDP port 53 answered The DNS operation timed out.; Server 132.167.198.5 UDP port 53 answered The DNS operation timed out.; Server 132.167.198.4 UDP port 53 answered The DNS operation timed out.; Server 132.167.198.5 UDP port 53 answered The DNS operation timed out.

I have tried connecting to MongoDB using MongoDB Compass, and it works perfectly fine. However, when I launch the Flask server, I encounter this error. Here’s how I am starting the server:

from api_folder.controller.main import app

if __name__ == "__main__":
    app.run(debug=True, host='0.0.0.0', port=8080)

I would appreciate any assistance or insights into resolving this issue. Thank you in advance for your help!

[EDIT]
pymongo version: 4.3.3
pymongo.MongoClient(“mongodb+srv://user:password@cluster0.qqawu.mongodb.net/myFirstDatabase?retryWrites=true&w=majority”)

Python version: 3.9.12
OS version: macOS Ventura 13.2.1

Hi Jayson,

I’m figuring out the same issue. Did you manage to fix it?