Guest User

Untitled

a guest
Dec 18th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. from pymongo import MongoClient
  2. from pymongo.errors import ConnectionFailure
  3. import os
  4. import logging
  5.  
  6. uri = os.getenv("MONGODB_URI", "")
  7. host = os.getenv("MONGODB_HOST", "localhost")
  8. port = os.getenv("MONGODB_PORT", 27017)
  9. db_name = os.getenv("MONGODB_NAME", "name")
  10.  
  11. try:
  12. if (uri != ""):
  13. db = MongoClient(uri)
  14. else:
  15. db = MongoClient(host, port=port,)
  16.  
  17. # Check that the server is available, requires no auth
  18. db.admin.command('ismaster')
  19.  
  20. # Connect to desired database
  21. db = db[db_name]
  22.  
  23. logging.info("Successfully connected to MongoDB")
  24.  
  25. except ConnectionFailure as e:
  26. logging.error("Error connecting to MongoDB. %s", e)
Add Comment
Please, Sign In to add comment