pyMongo connection slow with firewall

28 views
Skip to first unread message

Goran Pavles

unread,
Nov 21, 2017, 12:53:44 AM11/21/17
to mongodb-user
Hey guys

I am connecting to my mongoDB 3.4 that runs on another machine in the network with pyMongo 3.5.1.
If the firewall of that machine is On, the connection takes 40s, if it's Off, it is instant. (just to connect, no big queries are done)
When connecting with the mongo console or with roboMongo, the connection is instant even when the firewall is on.
All firewall settings are set for ports, ip of my machine, and mongod.exe.

What could it be that pyMongo is doing differently, that causes the firewall to prevent it from connecting for 40s?

This is all on windows 10.

Thanks

Goran

Bernie Hackett

unread,
Nov 21, 2017, 12:56:05 PM11/21/17
to mongodb-user
How are you measuring the time it takes to connect?

Goran Pavles

unread,
Nov 21, 2017, 7:03:47 PM11/21/17
to mongodb-user
Here's my code that connects and measures the time:

import pymongo
from time import time




def mongodb_connect(db_host, db_port):
   
"""
    Returns a MongoClient if client is running.
    Otherwise return None
    - db_host: the host name for the database
    - db_port: the port number (int) for the database
    """

   
try:
       
# If you increase this value to 3 000 000 then it will time out after 40sec
        maxSevSelDelay
= 3000
        client
= pymongo.MongoClient(db_host, int(db_port),
                                    serverSelectionTimeoutMS
=maxSevSelDelay)
       
# force connection by running a function
        client
.server_info()
       
return client
   
except pymongo.errors.ServerSelectionTimeoutError as err:
       
# do whatever you need
        msg
= ( "The specified MongoDB client doesn't seem to be running.\n"
               
"(host:{A}, port:{B})\n\n"
               
"Error:\n{C}\n".format(A=db_host, B=db_port, C=err)
               
)
       
print msg
       
return None




start
= time()
print "\nTrying to connect to MongoClient"
c
= mongodb_connect("ServerName", 27017)
print "test:", c.database_names()
print "Took {} seconds to reach client".format(time()-start)
Reply all
Reply to author
Forward
0 new messages