title: How can I fix pymongo server selection timeout error
body: <p>I am totally newbie to mongodb and pymongo. Yesterday for a small project I started to test how nosql might feels. but, I can't do anything with it because of server selection time out error</p>
  
  <p>This is the whole code</p>
  
  <pre class="lang-py prettyprint-override"><code>import pymongo
  
  client = pymongo.MongoClient("mongodb://localhost:27017/")
  
  ContactsDB = client["ContactsDB"]
  
  ContactsCollection = ContactsDB["ContactsCollection"]
  
  to_insert = { "name": f"{name}", "phone": f"{phone}"}
  
  ContactsCollection.insert_one(to_insert)
  </code></pre>
  
  <p>And this is the error </p>
  
  <pre><code>  File "main.py", line 20, in add_contact
      x = ContactsCollection.insert_one(mydict)
    File "/home/erfan2003/.local/lib/python3.6/site-packages/pymongo/collection.py", line 700, in insert_one
      session=session),
    File "/home/erfan2003/.local/lib/python3.6/site-packages/pymongo/collection.py", line 614, in _insert
      bypass_doc_val, session)
    File "/home/erfan2003/.local/lib/python3.6/site-packages/pymongo/collection.py", line 602, in _insert_one
      acknowledged, _insert_command, session)
    File "/home/erfan2003/.local/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1279, in _retryable_write
      with self._tmp_session(session) as s:
    File "/usr/lib/python3.6/contextlib.py", line 81, in __enter__
      return next(self.gen)
    File "/home/erfan2003/.local/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1611, in _tmp_session
      s = self._ensure_session(session)
    File "/home/erfan2003/.local/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1598, in _ensure_session
      return self.__start_session(True, causal_consistency=False)
    File "/home/erfan2003/.local/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1551, in __start_session
      server_session = self._get_server_session()
    File "/home/erfan2003/.local/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1584, in _get_server_session
      return self._topology.get_server_session()
    File "/home/erfan2003/.local/lib/python3.6/site-packages/pymongo/topology.py", line 429, in get_server_session
      None)
    File "/home/erfan2003/.local/lib/python3.6/site-packages/pymongo/topology.py", line 200, in _select_servers_loop
      self._error_message(selector))
  pymongo.errors.ServerSelectionTimeoutError: localhost:27027: [Errno 111] Connection refused
  </code></pre>