Python pymongo.errors.ConnectionFailure() Examples

The following are 30 code examples of pymongo.errors.ConnectionFailure(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pymongo.errors , or try the search function .
Example #1
Source File: mongodb.py    From pastepwn with MIT License 6 votes vote down vote up
def __init__(self, ip="127.0.0.1", port=27017, dbname="pastepwn", collectionname="pastes"):
        super().__init__()
        self.logger = logging.getLogger(__name__)
        self.logger.debug("Initializing MongoDB - {0}:{1}".format(ip, port))
        self.db = pymongo.MongoClient(ip, port, serverSelectionTimeoutMS=5000)

        try:
            self.db.admin.command("ismaster")
        except ConnectionFailure as e:
            self.logger.error(e)
            raise e

        self.logger.debug("Connected to database!")

        self.db = self.db[dbname]
        self.collection = self.db[collectionname]
        self.collection.create_index([('key', pymongo.ASCENDING)], unique=True) 
Example #2
Source File: datastore.py    From lightflow with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def add(self, payload=None):
        """ Adds a new document to the data store and returns its id.

        Args:
            payload (dict): Dictionary of initial data that should be stored
                in the new document in the meta section.

        Raises:
            DataStoreNotConnected: If the data store is not connected to the server.

        Returns:
            str: The id of the newly created document.
        """
        try:
            db = self._client[self.database]
            col = db[WORKFLOW_DATA_COLLECTION_NAME]
            return str(col.insert_one({
                DataStoreDocumentSection.Meta:
                    payload if isinstance(payload, dict) else {},
                DataStoreDocumentSection.Data: {}
            }).inserted_id)

        except ConnectionFailure:
            raise DataStoreNotConnected() 
Example #3
Source File: datastore.py    From lightflow with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def exists(self, workflow_id):
        """ Checks whether a document with the specified workflow id already exists.

        Args:
            workflow_id (str): The workflow id that should be checked.

        Raises:
            DataStoreNotConnected: If the data store is not connected to the server.

        Returns:
            bool: ``True`` if a document with the specified workflow id exists.
        """
        try:
            db = self._client[self.database]
            col = db[WORKFLOW_DATA_COLLECTION_NAME]
            return col.find_one({"_id": ObjectId(workflow_id)}) is not None

        except ConnectionFailure:
            raise DataStoreNotConnected() 
Example #4
Source File: mongo_client.py    From opsbro with MIT License 6 votes vote down vote up
def _reset_on_error(self, server, func, *args, **kwargs):
        """Execute an operation. Reset the server on network error.

        Returns fn()'s return value on success. On error, clears the server's
        pool and marks the server Unknown.

        Re-raises any exception thrown by fn().
        """
        try:
            return func(*args, **kwargs)
        except NetworkTimeout:
            # The socket has been closed. Don't reset the server.
            raise
        except ConnectionFailure:
            self.__reset_server(server.description.address)
            raise 
Example #5
Source File: mongo_client.py    From opsbro with MIT License 6 votes vote down vote up
def _get_socket(self, selector):
        server = self._get_topology().select_server(selector)
        try:
            with server.get_socket(self.__all_credentials) as sock_info:
                yield sock_info
        except NetworkTimeout:
            # The socket has been closed. Don't reset the server.
            # Server Discovery And Monitoring Spec: "When an application
            # operation fails because of any network error besides a socket
            # timeout...."
            raise
        except NotMasterError:
            # "When the client sees a "not master" error it MUST replace the
            # server's description with type Unknown. It MUST request an
            # immediate check of the server."
            self._reset_server_and_request_check(server.description.address)
            raise
        except ConnectionFailure:
            # "Client MUST replace the server's description with type Unknown
            # ... MUST NOT request an immediate check of the server."
            self.__reset_server(server.description.address)
            raise 
Example #6
Source File: mongodb.py    From CuckooSploit with GNU General Public License v3.0 6 votes vote down vote up
def connect(self):
        """Connects to Mongo database, loads options and set connectors.
        @raise CuckooReportError: if unable to connect.
        """
        host = self.options.get("host", "127.0.0.1")
        port = self.options.get("port", 27017)
        db = self.options.get("db", "cuckoo")

        try:
            self.conn = MongoClient(host, port)
            self.db = self.conn[db]
            self.fs = GridFS(self.db)
        except TypeError:
            raise CuckooReportError("Mongo connection port must be integer")
        except ConnectionFailure:
            raise CuckooReportError("Cannot connect to MongoDB") 
Example #7
Source File: vtEngine.py    From InplusTrader_Linux with MIT License 6 votes vote down vote up
def dbConnect(self):
        """连接MongoDB数据库"""
        if not self.dbClient:
            # 读取MongoDB的设置
            host, port, logging = loadMongoSetting()
                
            try:
                # 设置MongoDB操作的超时时间为0.5秒
                self.dbClient = MongoClient(host, port, connectTimeoutMS=500)
                
                # 调用server_info查询服务器状态,防止服务器异常并未连接成功
                self.dbClient.server_info()

                self.writeLog(u'MongoDB连接成功')
                
                # 如果启动日志记录,则注册日志事件监听函数
                if logging:
                    self.eventEngine.register(EVENT_LOG, self.dbLogging)
                    
            except ConnectionFailure:
                self.writeLog(u'MongoDB连接失败')
    
    #---------------------------------------------------------------------- 
Example #8
Source File: MongoService.py    From k8s-mongo-operator with GNU Affero General Public License v3.0 6 votes vote down vote up
def _executeAdminCommand(self, cluster_object: V1MongoClusterConfiguration, mongo_command: str, *args, **kwargs
                             ) -> Optional[Dict[str, any]]:
        """
        Executes the given mongo command on the MongoDB cluster.
        Retries a few times in case we receive a handshake failure.
        :param name: The name of the cluster.
        :param namespace: The namespace of the cluster.
        :param mongo_command: The command to be executed in mongo.
        :return: The response from MongoDB. See files in `tests/fixtures/mongo_responses` for examples.
        :raise ValueError: If the result could not be parsed.
        :raise TimeoutError: If we could not connect after retrying.
        """
        for _ in range(self.MONGO_COMMAND_RETRIES):
            try:
                name = cluster_object.metadata.name
                if name not in self._connected_replica_sets:
                    self._connected_replica_sets[name] = self._createMongoClientForReplicaSet(cluster_object)
                return self._connected_replica_sets[name].admin.command(mongo_command, *args, **kwargs)
            except ConnectionFailure as err:
                logging.error("Exception while trying to connect to Mongo: %s", str(err))
            logging.info("Command timed out, waiting %s seconds before trying again (attempt %s/%s)",
                         self.MONGO_COMMAND_WAIT, _, self.MONGO_COMMAND_RETRIES)
            sleep(self.MONGO_COMMAND_WAIT)

        raise TimeoutError("Could not execute command after {} retries!".format(self.MONGO_COMMAND_RETRIES)) 
Example #9
Source File: vtEngine.py    From chanlun with MIT License 6 votes vote down vote up
def dbConnect(self):
        """连接MongoDB数据库"""
        if not self.dbClient:
            # 读取MongoDB的设置
            host, port = loadMongoSetting()
                
            try:
                # 设置MongoDB操作的超时时间为0.5秒
                self.dbClient = MongoClient(host, port, serverSelectionTimeoutMS=500)
                
                # 调用server_info查询服务器状态,防止服务器异常并未连接成功
                self.dbClient.server_info()

                self.writeLog(u'MongoDB连接成功')
            except ConnectionFailure:
                self.writeLog(u'MongoDB连接失败')
    
    #---------------------------------------------------------------------- 
Example #10
Source File: datastore.py    From lightflow with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def remove(self, workflow_id):
        """ Removes a document specified by its id from the data store.

        All associated GridFs documents are deleted as well.

        Args:
            workflow_id (str): The id of the document that represents a workflow run.

        Raises:
            DataStoreNotConnected: If the data store is not connected to the server.
        """
        try:
            db = self._client[self.database]
            fs = GridFSProxy(GridFS(db.unproxied_object))

            for grid_doc in fs.find({"workflow_id": workflow_id},
                                    no_cursor_timeout=True):
                fs.delete(grid_doc._id)

            col = db[WORKFLOW_DATA_COLLECTION_NAME]
            return col.delete_one({"_id": ObjectId(workflow_id)})

        except ConnectionFailure:
            raise DataStoreNotConnected() 
Example #11
Source File: databaseManager.py    From CoAPthon with MIT License 6 votes vote down vote up
def delete(self, resource):
        """
        Delete an endpoint and all its resources
        :param resource: the registration resource to delete
        :return: the code of the response
        """
        res = {'res': resource}
        try:
            result = self.collection.delete_one(res)
            if not result.deleted_count:
                return defines.Codes.NOT_FOUND.number
            return defines.Codes.DELETED.number
        except ConnectionFailure:
            logger.error("Connection to the database cannot be made or is lost.")
            return defines.Codes.SERVICE_UNAVAILABLE.number
        except OperationFailure:
            logger.error("Delete operation failure on resource " + resource)
            return defines.Codes.SERVICE_UNAVAILABLE.number 
Example #12
Source File: databaseManager.py    From CoAPthon with MIT License 6 votes vote down vote up
def update(self, resource, uri_query):
        """
        Update a registration resource.
        :param resource: the resource to update
        :param uri_query: the parameters of the registration resource to update
        :return: the code of the response
        """
        if len(resource) <= 0:
            return defines.Codes.BAD_REQUEST.number
        data = {}
        if len(uri_query) > 0:
            data = self.parse_uri_query(uri_query)
        res = {'res': resource}
        try:
            data.update({"time": int(time())})
            result = self.collection.update_one(res, {"$set": data})
            if not result.matched_count:
                return defines.Codes.NOT_FOUND.number
            return defines.Codes.CHANGED.number
        except ConnectionFailure:
            logger.error("Connection to the database cannot be made or is lost.")
            return defines.Codes.SERVICE_UNAVAILABLE.number
        except OperationFailure:
            logger.error("Update operation failure on resource " + resource + " and with uri query " + uri_query)
            return defines.Codes.SERVICE_UNAVAILABLE.number 
Example #13
Source File: databaseManager.py    From CoAPthon with MIT License 6 votes vote down vote up
def search(self, uri_query, type_search):
        """
        Search endpoints or resources.
        :param uri_query: parameters to search
        :param type_search: it's equal to ep if the search is for endpoints or res if it is for resources
        :return: the string of results or an error code
        """
        if (type_search != "ep") and (type_search != "res"):
            return defines.Codes.BAD_REQUEST.number
        if len(uri_query) <= 0:
            uri_query = "ep=*"
        query = self.parse_uri_query(uri_query)
        query_rdp, query_res = self.split_queries(query)
        try:
            query = [{"$match": {"$and": [query_rdp, {"$expr": {"$gt": [{"$sum": ["$lt", "$time"]}, int(time())]}}]}},
                     {"$unwind": "$links"}, {"$match": query_res}]
            result = self.collection.aggregate(query)
            link = self.serialize_core_link_format(result, type_search)
            return link
        except ConnectionFailure:
            logger.error("Connection to the database cannot be made or is lost.")
            return defines.Codes.SERVICE_UNAVAILABLE.number
        except OperationFailure as e:
            logger.error("Search operation failure with type of search " + type_search + " and uri query " + uri_query + " " +e.message)
            return defines.Codes.SERVICE_UNAVAILABLE.number 
Example #14
Source File: mongo.py    From pytest-plugins with MIT License 6 votes vote down vote up
def check_server_up(self):
        """Test connection to the server."""
        import pymongo
        from pymongo.errors import AutoReconnect, ConnectionFailure

        # Hostname must exist before continuing
        # Some server class (e.g. Docker) will only allocate an IP after the
        # container has started.
        if not self.hostname:
            return False

        log.info("Connecting to Mongo at %s:%s" % (self.hostname, self.port))
        try:
            self.api = pymongo.MongoClient(self.hostname, self.port,
                                           serverselectiontimeoutms=200)
            self.api.list_database_names()
            # Configure the client with default timeouts in case the server goes slow
            self.api = pymongo.MongoClient(self.hostname, self.port)
            return True
        except (AutoReconnect, ConnectionFailure) as e:
            pass
        return False 
Example #15
Source File: mongostorage.py    From steemdata-mongo with MIT License 6 votes vote down vote up
def __init__(self, db_name=DB_NAME, host=MONGO_HOST, port=MONGO_PORT):
        try:
            mongo_url = 'mongodb://%s:%s/%s' % (host, port, db_name)
            client = pymongo.MongoClient(mongo_url)
            self.db = client[db_name]

        except ConnectionFailure as e:
            print('Can not connect to MongoDB server: %s' % e)
            raise
        else:
            self.Blockchain = self.db['Blockchain']
            self.Accounts = self.db['Accounts']
            self.Posts = self.db['Posts']
            self.Comments = self.db['Comments']
            self.Operations = self.db['Operations']
            self.AccountOperations = self.db['AccountOperations']
            self.PriceHistory = self.db['PriceHistory'] 
Example #16
Source File: vtEngine.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def dbUpdate(self, dbName, collectionName, d, flt, upsert=False):
        """向MongoDB中更新数据,d是具体数据,flt是过滤条件,upsert代表若无是否要插入"""
        try:
            if self.dbClient:
                db = self.dbClient[dbName]
                collection = db[collectionName]
                collection.replace_one(flt, d, upsert)
            else:
                self.writeLog(text.DATA_UPDATE_FAILED)
                if self.db_has_connected:
                    self.writeLog(u'重新尝试连接数据库')
                    self.dbConnect()

        except AutoReconnect as ex:
            self.writeError(u'数据库连接断开重连:{}'.format(str(ex)))
            time.sleep(1)
        except ConnectionFailure:
            self.dbClient = None
            self.writeError(u'数据库连接断开')
            if self.db_has_connected:
                self.writeLog(u'重新尝试连接数据库')
                self.dbConnect()
        except Exception as ex:
            self.writeError(u'dbUpdate exception:{}'.format(str(ex))) 
Example #17
Source File: pool.py    From recruit with Apache License 2.0 6 votes vote down vote up
def connect(self):
        """Connect to Mongo and return a new (connected) socket. Note that the
           pool does not keep a reference to the socket -- you must call
           return_socket() when you're done with it.
        """
        sock = self.create_connection()
        hostname = self.pair[0]

        if self.use_ssl:
            try:
                sock = ssl.wrap_socket(sock,
                                       certfile=self.ssl_certfile,
                                       keyfile=self.ssl_keyfile,
                                       ca_certs=self.ssl_ca_certs,
                                       cert_reqs=self.ssl_cert_reqs)
                if self.ssl_cert_reqs:
                    match_hostname(sock.getpeercert(), hostname)

            except ssl.SSLError:
                sock.close()
                raise ConnectionFailure("SSL handshake failed. MongoDB may "
                                        "not be configured with SSL support.")

        sock.settimeout(self.net_timeout)
        return SocketInfo(sock, self.pool_id, hostname) 
Example #18
Source File: mongo_client.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def _reset_on_error(self, server, func, *args, **kwargs):
        """Execute an operation. Reset the server on network error.

        Returns fn()'s return value on success. On error, clears the server's
        pool and marks the server Unknown.

        Re-raises any exception thrown by fn().
        """
        try:
            return func(*args, **kwargs)
        except NetworkTimeout:
            # The socket has been closed. Don't reset the server.
            raise
        except ConnectionFailure:
            self.__reset_server(server.description.address)
            raise 
Example #19
Source File: mongo_client.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def _is_writable(self):
        """Attempt to connect to a writable server, or return False.
        """
        topology = self._get_topology()  # Starts monitors if necessary.
        try:
            svr = topology.select_server(writable_server_selector)

            # When directly connected to a secondary, arbiter, etc.,
            # select_server returns it, whatever the selector. Check
            # again if the server is writable.
            return svr.description.is_writable
        except ConnectionFailure:
            return False 
Example #20
Source File: TestMongoService.py    From k8s-mongo-operator with GNU Affero General Public License v3.0 5 votes vote down vote up
def test__mongoAdminCommand_NoPrimary(self, mongo_client_mock):
        mongo_client_mock.return_value.admin.command.side_effect = (
            ConnectionFailure("No replica set members match selector \"Primary()\""),
            self._getFixture("initiate-ok"),
            self._getFixture("initiate-ok")

        )

        self.service._executeAdminCommand(self.cluster_object, "replSetGetStatus") 
Example #21
Source File: vtEngine2.py    From InplusTrader_Linux with MIT License 5 votes vote down vote up
def dbConnect(self):
        """连接MongoDB数据库"""
        if not self.dbClient:
            # 读取MongoDB的设置
            host, port = loadMongoSetting()
                
            try:
                # 设置MongoDB操作的超时时间为0.5秒
                self.dbClient = MongoClient(host, port, serverSelectionTimeoutMS=500)
                
                # 调用server_info查询服务器状态,防止服务器异常并未连接成功
                self.dbClient.server_info()
                self.writeLog(u'MongoDB连接成功')
            except ConnectionFailure:
                self.dbClient=None
                self.writeLog(u'MongoDB连接失败')

        """连接mysql数据库"""
        if not self.mysqlConnection :
            # 读取MongoDB的设置
            host, port,user,password ,dbname= loadMysqlSetting()
            try:
                self.mysqlConnection=MySQLdb.connect(host=host, user=user,
                                                         passwd=password, db=dbname, port=port)
                self.writeLog(u'连接MysqlDB成功')
            except :
                self.mysqlConnection=None
                self.writeLog(u'连接MysqlDB失败')
    
    #---------------------------------------------------------------------- 
Example #22
Source File: data.py    From piston-lib with MIT License 5 votes vote down vote up
def __init__(self,
                 db_name='SteemData',
                 host='steemit:steemit@mongo1.steemdata.com',
                 port=27017):
        try:
            self.mongo_url = 'mongodb://%s:%s/%s' % (host, port, db_name)
            client = pymongo.MongoClient(self.mongo_url)
            self.db = client[db_name]

        except ConnectionFailure as e:
            log.error('Can not connect to MongoDB server: %s' % e)
            raise
        else:
            self.load_collections() 
Example #23
Source File: mongo_client.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def max_pool_size(self):
        """The maximum allowable number of concurrent connections to each
        connected server. Requests to a server will block if there are
        `maxPoolSize` outstanding connections to the requested server.
        Defaults to 100. Cannot be 0.

        When a server's pool has reached `max_pool_size`, operations for that
        server block waiting for a socket to be returned to the pool. If
        ``waitQueueTimeoutMS`` is set, a blocked operation will raise
        :exc:`~pymongo.errors.ConnectionFailure` after a timeout.
        By default ``waitQueueTimeoutMS`` is not set.
        """
        return self.__options.pool_options.max_pool_size 
Example #24
Source File: from_0_6_to_1_1.py    From CuckooSploit with GNU General Public License v3.0 5 votes vote down vote up
def mongo_upgrade():
    """Migrate mongodb schema and data."""
    # Read reporting.conf to fetch mongo configuration.
    config = Config(cfg=os.path.join("..", "..", "conf", "reporting.conf"))
    # Run migration only if mongo is enabled as reporting module.
    if config.mongodb.enabled:
        host = config.mongodb.get("host", "127.0.0.1")
        port = config.mongodb.get("port", 27017)
        print "Mongo reporting is enabled, strarting mongo data migration."

        # Connect.
        try:
            conn = Connection(host, port)
            db = conn.cuckoo
        except TypeError:
            print "Mongo connection port must be integer"
            sys.exit()
        except ConnectionFailure:
            print "Cannot connect to MongoDB"
            sys.exit()

        # Check for schema version and create it.
        if "cuckoo_schema" in db.collection_names():
            print "Mongo schema version not expected"
            sys.exit()
        else:
            db.cuckoo_schema.save({"version": mongo_revision})

    else:
        print "Mongo reporting module not enabled, skipping mongo migration." 
Example #25
Source File: mongo_client.py    From opsbro with MIT License 5 votes vote down vote up
def max_pool_size(self):
        """The maximum allowable number of concurrent connections to each
        connected server. Requests to a server will block if there are
        `maxPoolSize` outstanding connections to the requested server.
        Defaults to 100. Cannot be 0.

        When a server's pool has reached `max_pool_size`, operations for that
        server block waiting for a socket to be returned to the pool. If
        ``waitQueueTimeoutMS`` is set, a blocked operation will raise
        :exc:`~pymongo.errors.ConnectionFailure` after a timeout.
        By default ``waitQueueTimeoutMS`` is not set.
        """
        return self.__options.pool_options.max_pool_size 
Example #26
Source File: mongo_client.py    From opsbro with MIT License 5 votes vote down vote up
def _is_writable(self):
        """Attempt to connect to a writable server, or return False.
        """
        topology = self._get_topology()  # Starts monitors if necessary.
        try:
            svr = topology.select_server(writable_server_selector)

            # When directly connected to a secondary, arbiter, etc.,
            # select_server returns it, whatever the selector. Check
            # again if the server is writable.
            return svr.description.is_writable
        except ConnectionFailure:
            return False 
Example #27
Source File: create_mongodb_index.py    From reviewer_experience_prediction with MIT License 5 votes vote down vote up
def main(argv=None):
    parser = ArgumentParser(description='Run incremental learning '
                                        'experiments.',
                            formatter_class=ArgumentDefaultsHelpFormatter,
                            conflict_handler='resolve')
    _add_arg = parser.add_argument
    _add_arg('-dbhost', '--mongodb_host',
             help='Host that the MongoDB server is running on.',
             type=str,
             default='localhost')
    _add_arg('--mongodb_port', '-dbport',
             help='Port that the MongoDB server is running on.',
             type=int,
             default=37017)
    args = parser.parse_args()

    # Imports
    import sys

    from pymongo import ASCENDING
    from pymongo.errors import ConnectionFailure

    from src.mongodb import connect_to_db

    # Connect to MongoDB database
    logger.info('Connecting to MongoDB database at {0}:{1}...'
                .format(args.mongodb_host, args.mongodb_port))
    try:
        db = connect_to_db(args.mongodb_host, args.mongodb_port)
    except ConnectionFailure as e:
        logger.error('Failed to connect to the MongoDB database collection.')
        raise e

    # Create index on 'steam_id_number' so that cursors can be sorted
    # on that particular key
    logger.info('Creating index on the "steam_id_number" key.')
    db.create_index('steam_id_number', ASCENDING)
    logger.info('Created new index named "steam_id_number_1" in the "reviews" '
                'collection.') 
Example #28
Source File: pool.py    From recruit with Apache License 2.0 5 votes vote down vote up
def _raise_wait_queue_timeout(self):
        raise ConnectionFailure(
            'Timed out waiting for socket from pool with max_size %r and'
            ' wait_queue_timeout %r' % (
                self.max_size, self.wait_queue_timeout)) 
Example #29
Source File: tokumx_stats.py    From cos-ansible-base with Apache License 2.0 5 votes vote down vote up
def mongodb_stats(host, p, database, username, password):
    port = int(p)
    try:
        if username and password and database:
            c = Client("mongodb://"+username+":"+password+"@"+host+"/"+database, port)
        elif username and password:
            c = Client('mongodb://'+username+':'+password+'@'+host+'/', port)
        elif database:
            c = Client('mongodb://'+host+'/'+database, port)
        else:
            c = Client(host, port)
    except ConnectionFailure, AutoReconnect:
        return None 
Example #30
Source File: mongodb_backend.py    From cti-taxii-server with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def catch_mongodb_error(func):
    """Catch mongodb availability error"""

    def api_wrapper(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except (ConnectionFailure, ServerSelectionTimeoutError) as e:
            raise MongoBackendError("Unable to connect to MongoDB", 500, e)

    return api_wrapper