Pytest -m connection fail

when i run test connection i get errors (runned in Anaconda Prompt, win10).
read a lot of posts about this - i did not found a solution.
with compass i’m able to connect.

the .ini file (as db name i tried test and sample_mflix with similar failing output):

# Ticket: Connection
# Rename this file to .ini after filling in your MFLIX_DB_URI and your SECRET_KEY
# Do not surround the URI with quotes

[PROD]
SECRET_KEY = super_secret_key_you_should_change
MFLIX_DB_URI = mongodb+srv://m220student:m220password@mflix.1jktv.mongodb.net/test?retryWrites=true&w=majority
MFLIX_NS = sample_mflix

[TEST]
SECRET_KEY = super_secret_testing_key
MFLIX_DB_URI = mongodb+srv://m220student:m220password@mflix.1jktv.mongodb.net/test?retryWrites=true&w=majority
MFLIX_NS = sample_mflix

the output:

(mflix) D:\pers\coding\python mongo\mflix-python>pytest -m connection
============================= test session starts =============================
platform win32 – Python 3.6.13, pytest-3.3.0, py-1.8.0, pluggy-0.6.0
rootdir: D:\pers\coding\python mongo\mflix-python, inifile: pytest.ini
plugins: flask-0.11.0
collected 43 items

tests\test_db_connection.py FFFF [100%]

================================== FAILURES ===================================
______________________________ test_atlas_setup _______________________________

client = <FlaskClient <Flask ‘mflix.factory’>>
config = <Config {‘DEBUG’: False, ‘TESTING’: False, ‘PROPAGATE_EXCEPTIONS’: True, ‘PRESERVE_CONTEXT_ON_EXCEPTION’: None, ‘SECRE…x’, ‘MFLIX_DB_URI’: ‘mongodb+srv://m220student:m220password@mflix.1jktv.mongodb.net/test?retryWrites=true&w=majority’}>

@pytest.mark.connection
@pytest.mark.usefixtures('config')
def test_atlas_setup(client, config):
  result = get_coll_names(config)

tests\test_db_connection.py:24:


tests\test_db_connection.py:18: in get_coll_names
return db.list_collection_names()
c:\users\liviup.conda\envs\mflix\lib\site-packages\pymongo\database.py:690: in list_collection_names
nameOnly=True)]
c:\users\liviup.conda\envs\mflix\lib\site-packages\pymongo\database.py:674: in list_collections
read_pref) as (sock_info, slave_okay):
c:\users\liviup.conda\envs\mflix\lib\contextlib.py:81: in enter
return next(self.gen)
c:\users\liviup.conda\envs\mflix\lib\site-packages\pymongo\mongo_client.py:1099: in _socket_for_reads
server = topology.select_server(read_preference)
c:\users\liviup.conda\envs\mflix\lib\site-packages\pymongo\topology.py:224: in select_server
address))
c:\users\liviup.conda\envs\mflix\lib\site-packages\pymongo\topology.py:183: in select_servers
selector, server_timeout, address)


self = <pymongo.topology.Topology object at 0x000001E443F39320>
selector = Primary(), timeout = 30, address = None

def _select_servers_loop(self, selector, timeout, address):
    """select_servers() guts. Hold the lock when calling this."""
    now = _time()
    end_time = now + timeout
    server_descriptions = self._description.apply_selector(
        selector, address)

    while not server_descriptions:
        # No suitable servers.
        if timeout == 0 or now > end_time:
            raise ServerSelectionTimeoutError(
              self._error_message(selector))

E pymongo.errors.ServerSelectionTimeoutError: SSL handshake failed: mflix-shard-00-00.1jktv.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852),SSL handshake failed: mflix-shard-00-02.1jktv.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852),SSL handshake failed: mflix-shard-00-01.1jktv.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)

c:\users\liviup.conda\envs\mflix\lib\site-packages\pymongo\topology.py:199: ServerSelectionTimeoutError
______________________________ test_basic_movies ______________________________

client = <FlaskClient <Flask ‘mflix.factory’>>

@pytest.mark.connection
def test_basic_movies(client):
  (actual, num_found) = get_movies({}, 0, 20)

tests\test_db_connection.py:31:


mflix\db.py:224: in get_movies
total_num_movies = db.movies.count_documents(query)
c:\users\liviup.conda\envs\mflix\lib\site-packages\pymongo\collection.py:1683: in count_documents
with self._socket_for_reads(session) as (sock_info, slave_ok):
c:\users\liviup.conda\envs\mflix\lib\contextlib.py:81: in enter
return next(self.gen)
c:\users\liviup.conda\envs\mflix\lib\site-packages\pymongo\mongo_client.py:1099: in _socket_for_reads
server = topology.select_server(read_preference)
c:\users\liviup.conda\envs\mflix\lib\site-packages\pymongo\topology.py:224: in select_server
address))
c:\users\liviup.conda\envs\mflix\lib\site-packages\pymongo\topology.py:183: in select_servers
selector, server_timeout, address)


self = <pymongo.topology.Topology object at 0x000001E444163BE0>
selector = Primary(), timeout = 30, address = None

def _select_servers_loop(self, selector, timeout, address):
    """select_servers() guts. Hold the lock when calling this."""
    now = _time()
    end_time = now + timeout
    server_descriptions = self._description.apply_selector(
        selector, address)

    while not server_descriptions:
        # No suitable servers.
        if timeout == 0 or now > end_time:
            raise ServerSelectionTimeoutError(
              self._error_message(selector))

E pymongo.errors.ServerSelectionTimeoutError: SSL handshake failed: mflix-shard-00-00.1jktv.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852),SSL handshake failed: mflix-shard-00-02.1jktv.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852),SSL handshake failed: mflix-shard-00-01.1jktv.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)

c:\users\liviup.conda\envs\mflix\lib\site-packages\pymongo\topology.py:199: ServerSelectionTimeoutError
___________________________ test_search_by_movie_id ___________________________

client = <FlaskClient <Flask ‘mflix.factory’>>

@pytest.mark.connection
def test_search_by_movie_id(client):
    actual = get_movie("573a13acf29313caabd29647")
  assert actual['title'] == 'King Kong'

E KeyError: ‘title’

tests\test_db_connection.py:39: KeyError
___________________________ test_simple_text_search ___________________________

client = <FlaskClient <Flask ‘mflix.factory’>>

@pytest.mark.connection
def test_simple_text_search(client):
  (actual, _) = get_movies({"$text": {"$search": "King Kong"}}, 0, 20)

tests\test_db_connection.py:44:


mflix\db.py:224: in get_movies
total_num_movies = db.movies.count_documents(query)
c:\users\liviup.conda\envs\mflix\lib\site-packages\pymongo\collection.py:1683: in count_documents
with self._socket_for_reads(session) as (sock_info, slave_ok):
c:\users\liviup.conda\envs\mflix\lib\contextlib.py:81: in enter
return next(self.gen)
c:\users\liviup.conda\envs\mflix\lib\site-packages\pymongo\mongo_client.py:1099: in _socket_for_reads
server = topology.select_server(read_preference)
c:\users\liviup.conda\envs\mflix\lib\site-packages\pymongo\topology.py:224: in select_server
address))
c:\users\liviup.conda\envs\mflix\lib\site-packages\pymongo\topology.py:183: in select_servers
selector, server_timeout, address)


self = <pymongo.topology.Topology object at 0x000001E4441B0160>
selector = Primary(), timeout = 30, address = None

def _select_servers_loop(self, selector, timeout, address):
    """select_servers() guts. Hold the lock when calling this."""
    now = _time()
    end_time = now + timeout
    server_descriptions = self._description.apply_selector(
        selector, address)

    while not server_descriptions:
        # No suitable servers.
        if timeout == 0 or now > end_time:
            raise ServerSelectionTimeoutError(
              self._error_message(selector))

E pymongo.errors.ServerSelectionTimeoutError: SSL handshake failed: mflix-shard-00-00.1jktv.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852),SSL handshake failed: mflix-shard-00-02.1jktv.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852),SSL handshake failed: mflix-shard-00-01.1jktv.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)

c:\users\liviup.conda\envs\mflix\lib\site-packages\pymongo\topology.py:199: ServerSelectionTimeoutError
============================= 39 tests deselected =============================
================== 4 failed, 39 deselected in 123.47 seconds ==================

(mflix) D:\pers\coding\python mongo\mflix-python>

Hi @liviu, welcome to the forums!

Can you verify the locally installed version of dnspython? PyMongo requires dnspython>=1.16<2. Please install+upgrade pymongo with the srv extra like this:

python -m pip install --upgrade 'pymongo[srv]'

See: https://pymongo.readthedocs.io/en/stable/installation.html#dependencies

Can you then re-try with dnspython and let us know if it works for you?

Kindest regards,
Eoin

1 Like

well, it seems that i am unable to run this command (win10):

(mflix) D:\pers\coding\python mongo\mflix-python>python -m pip install --upgrade ‘pymongo[srv]’
ERROR: Invalid requirement: “‘pymongo[srv]’”

i’ve found no command for retrieving the dnspython version.

Hi @liviu

Can you try re-running that command with double quotes around “pymongo[srv]” rather than single quotes?

Thanks!
Eoin

well, the command runned with success

(base) D:\pers\coding\python mongo\mflix-python>python -m pip install --upgrade "pymongo[srv]"
Collecting pymongo[srv]
  Downloading pymongo-3.11.4-cp38-cp38-win_amd64.whl (383 kB)
     |████████████████████████████████| 383 kB 2.2 MB/s
Collecting dnspython<2.0.0,>=1.16.0
  Downloading dnspython-1.16.0-py2.py3-none-any.whl (188 kB)
     |████████████████████████████████| 188 kB 2.2 MB/s
Installing collected packages: pymongo, dnspython
Successfully installed dnspython-1.16.0 pymongo-3.11.4

But it solved nothing, and now i cannot even access http://localhost:5000/

(mflix) D:\pers\coding\python mongo\mflix-python>python run.py
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 821-145-015
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [18/May/2021 10:46:15] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [18/May/2021 10:46:15] "GET /static/css/main.d2c98b4b.chunk.css HTTP/1.1" 200 -
127.0.0.1 - - [18/May/2021 10:46:15] "GET /static/js/1.908cc23a.chunk.js HTTP/1.1" 200 -
127.0.0.1 - - [18/May/2021 10:46:16] "GET /static/js/main.02d67aeb.chunk.js HTTP/1.1" 200 -
127.0.0.1 - - [18/May/2021 10:46:47] "GET /api/v1/movies/ HTTP/1.1" 500 -
Traceback (most recent call last):
  File "C:\Users\liviup\.conda\envs\mflix\lib\site-packages\flask\app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Users\liviup\.conda\envs\mflix\lib\site-packages\flask\app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Users\liviup\.conda\envs\mflix\lib\site-packages\flask_cors\extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "C:\Users\liviup\.conda\envs\mflix\lib\site-packages\flask\app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\liviup\.conda\envs\mflix\lib\site-packages\flask\_compat.py", line 33, in reraise
    raise value
  File "C:\Users\liviup\.conda\envs\mflix\lib\site-packages\flask\app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\liviup\.conda\envs\mflix\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\liviup\.conda\envs\mflix\lib\site-packages\flask_cors\extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "C:\Users\liviup\.conda\envs\mflix\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\liviup\.conda\envs\mflix\lib\site-packages\flask\_compat.py", line 33, in reraise
    raise value
  File "C:\Users\liviup\.conda\envs\mflix\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\liviup\.conda\envs\mflix\lib\site-packages\flask\app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "D:\pers\coding\python mongo\mflix-python\mflix\api\movies.py", line 26, in api_get_movies
    None, page=0, movies_per_page=MOVIES_PER_PAGE)
  File "D:\pers\coding\python mongo\mflix-python\mflix\db.py", line 224, in get_movies
    total_num_movies = db.movies.count_documents(query)
  File "C:\Users\liviup\.conda\envs\mflix\lib\site-packages\pymongo\collection.py", line 1683, in count_documents
    with self._socket_for_reads(session) as (sock_info, slave_ok):
  File "C:\Users\liviup\.conda\envs\mflix\lib\contextlib.py", line 81, in __enter__
    return next(self.gen)
  File "C:\Users\liviup\.conda\envs\mflix\lib\site-packages\pymongo\mongo_client.py", line 1099, in _socket_for_reads
    server = topology.select_server(read_preference)
  File "C:\Users\liviup\.conda\envs\mflix\lib\site-packages\pymongo\topology.py", line 224, in select_server
    address))
  File "C:\Users\liviup\.conda\envs\mflix\lib\site-packages\pymongo\topology.py", line 183, in select_servers
    selector, server_timeout, address)
  File "C:\Users\liviup\.conda\envs\mflix\lib\site-packages\pymongo\topology.py", line 199, in _select_servers_loop
    self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: SSL handshake failed: mflix-shard-00-02.1jktv.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852),SSL handshake failed: mflix-shard-00-00.1jktv.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852),SSL handshake failed: mflix-shard-00-01.1jktv.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)
127.0.0.1 - - [18/May/2021 10:47:17] "GET /api/v1/movies/facet-search?cast=Denzel%20Washington&page=0 HTTP/1.1" 400 -
127.0.0.1 - - [18/May/2021 10:47:17] "GET /static/media/mongoleaf.0ebc1843.png HTTP/1.1" 200 -
127.0.0.1 - - [18/May/2021 10:47:18] "GET /manifest.json HTTP/1.1" 200 -
127.0.0.1 - - [18/May/2021 10:47:18] "GET /favicon.ico HTTP/1.1" 200 -
 * Detected change in 'C:\\Users\\liviup\\.conda\\envs\\mflix\\lib\\site-packages\\bson\\__init__.py', reloading
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 821-145-015
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

![mflix|690x292]

although i see small differences between the error results:

(mflix) D:\pers\coding\python mongo\mflix-python>pytest -m connection
============================= test session starts =============================
platform win32 -- Python 3.6.13, pytest-3.3.0, py-1.8.0, pluggy-0.6.0
rootdir: D:\pers\coding\python mongo\mflix-python, inifile: pytest.ini
plugins: flask-0.11.0
collected 43 items

tests\test_db_connection.py FFFF                                         [100%]

================================== FAILURES ===================================
______________________________ test_atlas_setup _______________________________

client = <FlaskClient <Flask 'mflix.factory'>>
config = <Config {'DEBUG': False, 'TESTING': False, 'PROPAGATE_EXCEPTIONS': True, 'PRESERVE_CONTEXT_ON_EXCEPTION': None, 'SECRE...IX_DB_URI': 'mongodb+srv://m220student:m220password@mflix.1jktv.mongodb.net/sample_mflix?retryWrites=true&w=majority'}>

    @pytest.mark.connection
    @pytest.mark.usefixtures('config')
    def test_atlas_setup(client, config):
>       result = get_coll_names(config)

tests\test_db_connection.py:24:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
tests\test_db_connection.py:18: in get_coll_names
    return db.list_collection_names()
c:\users\liviup\.conda\envs\mflix\lib\site-packages\pymongo\database.py:863: in list_collection_names
    for result in self.list_collections(session=session, **kwargs)]
c:\users\liviup\.conda\envs\mflix\lib\site-packages\pymongo\database.py:826: in list_collections
    _cmd, read_pref, session)
c:\users\liviup\.conda\envs\mflix\lib\site-packages\pymongo\mongo_client.py:1461: in _retryable_read
    read_pref, session, address=address)
c:\users\liviup\.conda\envs\mflix\lib\site-packages\pymongo\mongo_client.py:1278: in _select_server
    server = topology.select_server(server_selector)
c:\users\liviup\.conda\envs\mflix\lib\site-packages\pymongo\topology.py:243: in select_server
    address))
c:\users\liviup\.conda\envs\mflix\lib\site-packages\pymongo\topology.py:200: in select_servers
    selector, server_timeout, address)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <Topology <TopologyDescription id: 60a373cf0c773bef036cf57a, topology_type: ReplicaSetNoPrimary, servers: [<ServerDesc...lix-shard-00-02.1jktv.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)',)>]>>
selector = Primary(), timeout = 30, address = None

    def _select_servers_loop(self, selector, timeout, address):
        """select_servers() guts. Hold the lock when calling this."""
        now = _time()
        end_time = now + timeout
        server_descriptions = self._description.apply_selector(
            selector, address, custom_selector=self._settings.server_selector)

        while not server_descriptions:
            # No suitable servers.
            if timeout == 0 or now > end_time:
                raise ServerSelectionTimeoutError(
                    "%s, Timeout: %ss, Topology Description: %r" %
>                   (self._error_message(selector), timeout, self.description))
E               pymongo.errors.ServerSelectionTimeoutError: SSL handshake failed: mflix-shard-00-00.1jktv.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852),SSL handshake failed: mflix-shard-00-01.1jktv.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852),SSL handshake failed: mflix-shard-00-02.1jktv.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852), Timeout: 30s, Topology Description: <TopologyDescription id: 60a373cf0c773bef036cf57a, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('mflix-shard-00-00.1jktv.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('SSL handshake failed: mflix-shard-00-00.1jktv.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)',)>, <ServerDescription ('mflix-shard-00-01.1jktv.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('SSL handshake failed: mflix-shard-00-01.1jktv.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)',)>, <ServerDescription ('mflix-shard-00-02.1jktv.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('SSL handshake failed: mflix-shard-00-02.1jktv.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)',)>]>

c:\users\liviup\.conda\envs\mflix\lib\site-packages\pymongo\topology.py:217: ServerSelectionTimeoutError
______________________________ test_basic_movies ______________________________

client = <FlaskClient <Flask 'mflix.factory'>>

    @pytest.mark.connection
    def test_basic_movies(client):
>       (actual, num_found) = get_movies({}, 0, 20)

tests\test_db_connection.py:31:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
mflix\db.py:224: in get_movies
    total_num_movies = db.movies.count_documents(query)
c:\users\liviup\.conda\envs\mflix\lib\site-packages\pymongo\collection.py:1786: in count_documents
    _cmd, self._read_preference_for(session), session)
c:\users\liviup\.conda\envs\mflix\lib\site-packages\pymongo\mongo_client.py:1461: in _retryable_read
    read_pref, session, address=address)
c:\users\liviup\.conda\envs\mflix\lib\site-packages\pymongo\mongo_client.py:1278: in _select_server
    server = topology.select_server(server_selector)
c:\users\liviup\.conda\envs\mflix\lib\site-packages\pymongo\topology.py:243: in select_server
    address))
c:\users\liviup\.conda\envs\mflix\lib\site-packages\pymongo\topology.py:200: in select_servers
    selector, server_timeout, address)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <Topology <TopologyDescription id: 60a373ed0c773bef036cf57b, topology_type: ReplicaSetNoPrimary, servers: [<ServerDesc...lix-shard-00-02.1jktv.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)',)>]>>
selector = Primary(), timeout = 30, address = None

    def _select_servers_loop(self, selector, timeout, address):
        """select_servers() guts. Hold the lock when calling this."""
        now = _time()
        end_time = now + timeout
        server_descriptions = self._description.apply_selector(
            selector, address, custom_selector=self._settings.server_selector)

        while not server_descriptions:
            # No suitable servers.
            if timeout == 0 or now > end_time:
                raise ServerSelectionTimeoutError(
                    "%s, Timeout: %ss, Topology Description: %r" %
>                   (self._error_message(selector), timeout, self.description))
E               pymongo.errors.ServerSelectionTimeoutError: SSL handshake failed: mflix-shard-00-00.1jktv.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852),SSL handshake failed: mflix-shard-00-01.1jktv.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852),SSL handshake failed: mflix-shard-00-02.1jktv.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852), Timeout: 30s, Topology Description: <TopologyDescription id: 60a373ed0c773bef036cf57b, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('mflix-shard-00-00.1jktv.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('SSL handshake failed: mflix-shard-00-00.1jktv.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)',)>, <ServerDescription ('mflix-shard-00-01.1jktv.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('SSL handshake failed: mflix-shard-00-01.1jktv.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)',)>, <ServerDescription ('mflix-shard-00-02.1jktv.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('SSL handshake failed: mflix-shard-00-02.1jktv.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)',)>]>

c:\users\liviup\.conda\envs\mflix\lib\site-packages\pymongo\topology.py:217: ServerSelectionTimeoutError
___________________________ test_search_by_movie_id ___________________________

client = <FlaskClient <Flask 'mflix.factory'>>

    @pytest.mark.connection
    def test_search_by_movie_id(client):
        actual = get_movie("573a13acf29313caabd29647")
>       assert actual['title'] == 'King Kong'
E       KeyError: 'title'

tests\test_db_connection.py:39: KeyError
___________________________ test_simple_text_search ___________________________

client = <FlaskClient <Flask 'mflix.factory'>>

    @pytest.mark.connection
    def test_simple_text_search(client):
>       (actual, _) = get_movies({"$text": {"$search": "King Kong"}}, 0, 20)

tests\test_db_connection.py:44:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
mflix\db.py:224: in get_movies
    total_num_movies = db.movies.count_documents(query)
c:\users\liviup\.conda\envs\mflix\lib\site-packages\pymongo\collection.py:1786: in count_documents
    _cmd, self._read_preference_for(session), session)
c:\users\liviup\.conda\envs\mflix\lib\site-packages\pymongo\mongo_client.py:1461: in _retryable_read
    read_pref, session, address=address)
c:\users\liviup\.conda\envs\mflix\lib\site-packages\pymongo\mongo_client.py:1278: in _select_server
    server = topology.select_server(server_selector)
c:\users\liviup\.conda\envs\mflix\lib\site-packages\pymongo\topology.py:243: in select_server
    address))
c:\users\liviup\.conda\envs\mflix\lib\site-packages\pymongo\topology.py:200: in select_servers
    selector, server_timeout, address)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <Topology <TopologyDescription id: 60a3742a0c773bef036cf57d, topology_type: ReplicaSetNoPrimary, servers: [<ServerDesc...lix-shard-00-02.1jktv.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)',)>]>>
selector = Primary(), timeout = 30, address = None

    def _select_servers_loop(self, selector, timeout, address):
        """select_servers() guts. Hold the lock when calling this."""
        now = _time()
        end_time = now + timeout
        server_descriptions = self._description.apply_selector(
            selector, address, custom_selector=self._settings.server_selector)

        while not server_descriptions:
            # No suitable servers.
            if timeout == 0 or now > end_time:
                raise ServerSelectionTimeoutError(
                    "%s, Timeout: %ss, Topology Description: %r" %
>                   (self._error_message(selector), timeout, self.description))
E               pymongo.errors.ServerSelectionTimeoutError: SSL handshake failed: mflix-shard-00-00.1jktv.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852),SSL handshake failed: mflix-shard-00-01.1jktv.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852),SSL handshake failed: mflix-shard-00-02.1jktv.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852), Timeout: 30s, Topology Description: <TopologyDescription id: 60a3742a0c773bef036cf57d, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('mflix-shard-00-00.1jktv.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('SSL handshake failed: mflix-shard-00-00.1jktv.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)',)>, <ServerDescription ('mflix-shard-00-01.1jktv.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('SSL handshake failed: mflix-shard-00-01.1jktv.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)',)>, <ServerDescription ('mflix-shard-00-02.1jktv.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('SSL handshake failed: mflix-shard-00-02.1jktv.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)',)>]>

c:\users\liviup\.conda\envs\mflix\lib\site-packages\pymongo\topology.py:217: ServerSelectionTimeoutError
============================= 39 tests deselected =============================
================== 4 failed, 39 deselected in 122.24 seconds ==================

Hi @liviu

Thanks for installing dnspython, it looks like this resolved part of the error. It appears that there are still certification issues and to ensure your Python environment has the latest certificate, I’d recommend install the certifi library:

pip install certifi

Can you install certifi and re-try as dnspython will resolve the SRV issues and certifi should resolve the remaining SSL certificate issues?

Kindest regards,
Eoin