Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from pymongo import MongoClient
- from pymongo.errors import ConnectionFailure
- import os
- import logging
- uri = os.getenv("MONGODB_URI", "")
- host = os.getenv("MONGODB_HOST", "localhost")
- port = os.getenv("MONGODB_PORT", 27017)
- db_name = os.getenv("MONGODB_NAME", "name")
- try:
- if (uri != ""):
- db = MongoClient(uri)
- else:
- db = MongoClient(host, port=port,)
- # Check that the server is available, requires no auth
- db.admin.command('ismaster')
- # Connect to desired database
- db = db[db_name]
- logging.info("Successfully connected to MongoDB")
- except ConnectionFailure as e:
- logging.error("Error connecting to MongoDB. %s", e)
Add Comment
Please, Sign In to add comment