def mongo_connect(host=None, port=None,ssl=False, user=None,passwd=None,replica=None):
try:
# ssl connection for pymongo > 2.1
if pymongo.version >= "2.1":
if replica is None:
con = pymongo.Connection(host, port, read_preference=pymongo.ReadPreference.SECONDARY, ssl=ssl, network_timeout=10)
else:
con = pymongo.Connection(host, port, read_preference=pymongo.ReadPreference.SECONDARY, ssl=ssl, replicaSet=replica, network_timeout=10)
else:
if replica is None:
con = pymongo.Connection(host, port, slave_okay=True, network_timeout=10)
else:
con = pymongo.Connection(host, port, slave_okay=True, replicaSet=replica, network_timeout=10)
if user and passwd:
db = con["admin"]
if not db.authenticate(user, passwd): sys.exit("Username/Password incorrect")
except Exception, e:
if isinstance(e,pymongo.errors.AutoReconnect) and str(e).find(" is an arbiter") != -1:
# We got a pymongo AutoReconnect exception that tells us we connected to an Arbiter Server
# This means: Arbiter is reachable and can answer requests/votes - this is all we need to know from an arbiter
print "OK - State: 7 (Arbiter)"
sys.exit(0)
return exit_with_general_critical(e),None
return 0,con
评论列表
文章目录