common.py 文件源码

python
阅读 18 收藏 0 点赞 0 评论 0

项目:picoCTF 作者: picoCTF 项目源码 文件源码
def get_conn():
    """
    Get a database connection

    Ensures that only one global database connection exists per thread.
    If the connection does not exist a new one is created and returned.
    """

    global __client, __connection
    if not __connection:
        try:
            # Allow more complex mongodb connections
            conf = api.app.app.config
            if conf["MONGO_USER"] and conf["MONGO_PW"]:
                uri = "mongodb://{}:{}@{}:{}/{}?authMechanism=SCRAM-SHA-1".format(
                        conf["MONGO_USER"],
                        conf["MONGO_PW"],
                        conf["MONGO_ADDR"],
                        conf["MONGO_PORT"],
                        conf["MONGO_DB_NAME"])
            else:
                uri = "mongodb://{}:{}/{}".format(
                        conf["MONGO_ADDR"],
                        conf["MONGO_PORT"],
                        conf["MONGO_DB_NAME"])

            __client = MongoClient(uri)
            __connection = __client[conf["MONGO_DB_NAME"]]
        except ConnectionFailure:
            raise SevereInternalException("Could not connect to mongo database {} at {}:{}".format(mongo_db_name, mongo_addr, mongo_port))
        except InvalidName as error:
            raise SevereInternalException("Database {} is invalid! - {}".format(mongo_db_name, error))

    return __connection
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号