我们从Python开源项目中,提取了以下48个代码示例,用于说明如何使用pymongo.errors.ConnectionFailure()。
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)
def connect(self): try: logging.debug("Getting MongoDB connection to %s (replicaSet=%s, readPreference=%s, readPreferenceTags=%s, ssl=%s)" % ( self.uri, self.replset, self.read_pref, self.do_rp_tags, self.do_ssl(), )) conn = MongoClient(**self.client_opts()) if self.do_connect: conn['admin'].command({"ping": 1}) except (ConnectionFailure, OperationFailure, ServerSelectionTimeoutError), e: logging.error("Unable to connect to %s! Error: %s" % (self.uri, e)) raise DBConnectionError(e) if conn is not None: self._conn = conn return self._conn
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. """ if external_client is not None: return external_client global __client, __connection if not __connection: try: __client = MongoClient(mongo_addr, mongo_port) __connection = __client[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
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????') #----------------------------------------------------------------------
def read_loop(self) -> None: while True: try: await self._read_loop_step() except asyncio.CancelledError: self._shut_down() return except Exception as e: self.__connected.clear() connection_error = ConnectionFailure('Connection was lost due to: {}'.format(str(e))) self.close(error=connection_error) for ft in self.__request_futures.values(): ft.set_exception(connection_error) self.__request_futures = {} try: await self.reconnect() except asyncio.CancelledError: self._shut_down() return
def connect(self): """????""" try: self.recorder('INFO', '{obj} connect start'.format(obj=self)) self.set_idle() self._client = MongoClient(**self.setting) if self.db: self.select_db(self.db) self.isConnect = True self.recorder('INFO', '{obj} connect successful'.format(obj=self)) except ConnectionFailure as e: self.recorder('ERROR', '{obj} connect failed [{msg}]'.format(obj=self, msg=e)) self.error() raise MongoError return self
def connect(self): """????""" try: self.recorder('INFO', '{obj} connect start'.format(obj=self)) self.set_idle() self._client = motor_tornado.MotorClient(**self.setting) if self.db: self.select_db(self.db) self.isConnect = True self.recorder('INFO', '{obj} connect successful'.format(obj=self)) except ConnectionFailure as e: self.recorder('ERROR', '{obj} connect failed [{msg}]'.format(obj=self, msg=e)) self.error() raise MongoError raise Return(self)
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 = int(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")
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()
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()
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()
def get(self, workflow_id): """ Returns the document for the given workflow id. 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. Returns: DataStoreDocument: The document for the given workflow id. """ try: db = self._client[self.database] fs = GridFSProxy(GridFS(db.unproxied_object)) return DataStoreDocument(db[WORKFLOW_DATA_COLLECTION_NAME], fs, workflow_id) except ConnectionFailure: raise DataStoreNotConnected()
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
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
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']
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
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))
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??') #----------------------------------------------------------------------
def _shut_down(self) -> None: connection_error = ConnectionFailure('Shutting down.') for ft in self.__request_futures.values(): ft.set_exception(connection_error) self.__disconnected.set()
def __init__(self): client = MongoClient(serverSelectionTimeoutMS=2000) self.connection_status = True try: # The ismaster command is cheap and does not require auth. client.admin.command('ismaster') except ConnectionFailure: print("ERROR : Connection Could Not Be established") self.connection_status = False self.db = client['Captains_Log_DB']
def __init__(self, uri): try: self.client = MongoClient(uri) # The ismaster command is cheap and does not require auth. self.client.admin.command('ismaster') except ConnectionFailure: print("Mongo DB not available")
def is_connected(self): """ Returns the connection status of the data store. Returns: bool: True if the data store is connected to the MongoDB server. """ if self._client is not None: try: self._client.server_info() except ConnectionFailure: return False return True else: return False
def get_client(host: str, port: int) -> MongoClient: try: client = MongoClient(host, port, maxPoolSize=MAX_POOL_SIZE) log.info("Connected successfully!!!") return client except errors.ConnectionFailure as e: log.error(e)
def connect(self): """ connect to mongdb database """ try: self.connection = MongoClient(self.host, self.port, connect=False) except errors.ConnectionFailure, e: logging.warn('%s : Connection Problem!', self.db_name) return self.connection
def connect_to_collection(self, collection_name): """ connect directly to mongodb collection :param collection_name: name of mongodb's collection :type collection_name: :py:class:`str` """ try: connection = MongoClient(self.host, self.port) records = connection[self.db_name ][collection_name] except errors.ConnectionFailure, e: logging.warn('%s : Connection Problem!', self.db_name) records = None return records
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] except TypeError: raise CuckooReportError("Mongo connection port must be integer") except ConnectionFailure: raise CuckooReportError("Cannot connect to MongoDB")
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
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
def main(): # ??????????localhost?????27017??????????/?????? try: client = MongoClient('localhost', 27017) print('Connected Successfully!') except ConnectionFailure as e: sys.stderr.write('Could not connect to MongoDB: %s' % e) sys.exit(1) db = client.local collection = db['CNN-Sentence-Pairs-Classification-Original'] collection.update({}, {}, safe=True) # create_collection(collection=collection, input_files_list=files_list)
def test_eptid_mongo_db(): try: edb = EptidMDB("secret", "idp") except ConnectionFailure: pass else: try: e1 = edb.get("idp_entity_id", "sp_entity_id", "user_id", "some other data") except ServerSelectionTimeoutError: pass else: print(e1) assert e1.startswith("idp_entity_id!sp_entity_id!") e2 = edb.get("idp_entity_id", "sp_entity_id", "user_id", "some other data") assert e1 == e2 e3 = edb.get("idp_entity_id", "sp_entity_id", "user_2", "some other data") print(e3) assert e1 != e3 e4 = edb.get("idp_entity_id", "sp_entity_id2", "user_id", "some other data") assert e4 != e1 assert e4 != e3
def ping(self): try: await self._conn.admin.command({'ping': 1}) return True except ConnectionFailure: return False
def __init__(self): self.__cfgReporting = Config(os.path.join(RAGPICKER_ROOT, 'config', 'reporting.conf')) self.__cfgProcessing = Config(os.path.join(RAGPICKER_ROOT, 'config', 'processing.conf')) self.__mongodbEnabled = self.__cfgReporting.getOption("mongodb", "enabled") self.__codedbEnabled = self.__cfgReporting.getOption("codeDB", "enabled") self.__bluecoatEnabled = self.__cfgProcessing.getOption("all_bluecoatMalwareAnalysisAppliance", "enabled") if self.__mongodbEnabled: #Anbindung an Datenbank MongoDB Collection Ragpicker herstellen try: mongodbHost = self.__cfgReporting.getOption("mongodb", "host") mongodbPort = self.__cfgReporting.getOption("mongodb", "port") self.__mongodbConnection = Connection(mongodbHost, mongodbPort) self.__mongodbCollectionRagpicker = self.__mongodbConnection.MalwareAnalyse.ragpicker self.__mongodbCollectionFamilies = self.__mongodbConnection.MalwareAnalyse.families self.__mongodbCollectionSandboxTaskQueue = self.__mongodbConnection.MalwareAnalyse.sandboxTaskQueue except TypeError: raise Exception("MongoDB connection port in report.config must be integer") except ConnectionFailure: raise Exception("Cannot connect to MongoDB (ragpicker)") if self.__codedbEnabled: #Anbindung an Datenbank MongoDB Collection CodeDB herstellen try: codedbHost = self.__cfgReporting.getOption("codeDB", "mongo_db_host") codedbPort = self.__cfgReporting.getOption("codeDB", "mongo_db_port") self.__codedbConnection = Connection(codedbHost, codedbPort) self.__codedbCollectionCodedb = self.__codedbConnection.MalwareAnalyse.codeDB except TypeError: raise Exception("MongoDB connection port for CodeDB in report.config must be integer") except ConnectionFailure: raise Exception("Cannot connect to MongoDB (codeDB)")
def create_connection(self): """Connect and return a socket object. This is a modified version of create_connection from CPython >=2.6. """ host, port = self.pair # Check if dealing with a unix domain socket if host.endswith('.sock'): if not hasattr(socket, "AF_UNIX"): raise ConnectionFailure("UNIX-sockets are not supported " "on this system") sock = socket.socket(socket.AF_UNIX) try: sock.connect(host) return sock except socket.error, e: if sock is not None: sock.close() raise e # Don't try IPv6 if we don't support it. Also skip it if host # is 'localhost' (::1 is fine). Avoids slow connect issues # like PYTHON-356. family = socket.AF_INET if socket.has_ipv6 and host != 'localhost': family = socket.AF_UNSPEC err = None for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM): af, socktype, proto, dummy, sa = res sock = None try: sock = socket.socket(af, socktype, proto) sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) sock.settimeout(self.conn_timeout or 20.0) sock.connect(sa) return sock except socket.error, e: err = e if sock is not None: sock.close() if err is not None: raise err else: # This likely means we tried to connect to an IPv6 only # host with an OS/kernel or Python interpreter that doesn't # support IPv6. The test case is Jython2.5.1 which doesn't # support IPv6 at all. raise socket.error('getaddrinfo failed')
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) database = config.mongodb.get("db", "cuckoo") print "Mongo reporting is enabled, strarting mongo data migration." if not port.isnumber(): print "Port must be an integer" sys.exit() # Support old Mongo. try: from pymongo.connection import Connection from pymongo.errors import ConnectionFailure conn = Connection(host, port) db = conn.cuckoo done = True except ImportError: print "Unable to import pymongo (install with `pip install pymongo`)" done = False except ConnectionFailure: print "Cannot connect to MongoDB" sys.exit() try: if not done: import pymongo try: db = pymongo.MongoClient(host, port)[database] except pymongo.errors.ConnectionFailure: print "Cannot connect to MongoDB" sys.exit() except ImportError: print "Unable to import pymongo (install with `pip install pymongo`)" 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."