我们从Python开源项目中,提取了以下29个代码示例,用于说明如何使用pymongo.errors.ServerSelectionTimeoutError()。
def select_server_by_address(self, address, server_selection_timeout=None): """Return a Server for "address", reconnecting if necessary. If the server's type is not known, request an immediate check of all servers. Time out after "server_selection_timeout" if the server cannot be reached. :Parameters: - `address`: A (host, port) pair. - `server_selection_timeout` (optional): maximum seconds to wait. If not provided, the default value common.SERVER_SELECTION_TIMEOUT is used. Calls self.open() if needed. Raises exc:`ServerSelectionTimeoutError` after `server_selection_timeout` if no matching servers are found. """ return self.select_server(any_server_selector, server_selection_timeout, address)
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 search_entries_by_title(self, title): """Search For a Specified Title in the Entries_Table Args: title(str): the title you are searching for Return: result(collection): the search result """ entries_table = self.db["Entries_Table"] try: result = entries_table.find_one({"Title": title}) return result except errors.ServerSelectionTimeoutError: print('ERROR : No connection could be made because' ' the target machine actively refused it') return False # TODO Modify to allow multiple results using find(), # TODO also find similar results which are not exact matches
def search_entries_by_created_date(self, date): """Search For Entries created on the specified Date in the Entries_Table Args: date(datetime): the date you are searching for Return: result(collection): the search result """ if date.date() <= datetime.now().date(): entries_table = self.db["Entries_Table"] try: return entries_table.find( {"Date_Created": date}) except errors.ServerSelectionTimeoutError: print('ERROR : No connection could be made because' ' the target machine actively refused it') return True return False
def search_entries_by_id(self, id): """Search For Entries created on the specified Date in the Entries_Table Args: id(string): the objectID you are searching for Return: result(collection): the search result """ entries_table = self.db["Entries_Table"] try: return entries_table.find( {"_id": ObjectId(id)}) except errors.ServerSelectionTimeoutError: print('ERROR : No connection could be made because' ' the target machine actively refused it') return True return False
def search_entries_by_modified_date(self, date): """Search For Entries modified on the specified Date in the Entries_Table Args: date(datetime): the date you are searching for Return: result(collection): the search result """ if date.date() <= datetime.now().date(): entries_table = self.db["Entries_Table"] try: return entries_table.find( {"Last_Modified": date}) except errors.ServerSelectionTimeoutError: print('ERROR : No connection could be made because' ' the target machine actively refused it') return True return False
def delete_entries(self, _id): """Delete entries in the Entries_Table Args: _id(str): Object ID of the entry you want to change Return: result(bool):true if the delete was successful. false if it fails """ entries_table = self.db["Entries_Table"] try: if not entries_table.find_one({"_id": ObjectId(_id)}): # print "The specified entry does not Exist" return False entries_table.delete_one({"_id": ObjectId(_id)}) return True except errors.ServerSelectionTimeoutError: print('ERROR : No connection could be made because' ' the target machine actively refused it') return False
def get_all(self): """ Returns: result(collection): all elements in the Entries Table """ try: entries_table = self.db["Entries_Table"] result = entries_table.find() print result[0] return result except errors.ServerSelectionTimeoutError: print('ERROR : No connection could be made because' 'the target machine actively refused it') return None
def support_func_get_all(self, lim): """ Args: lim(int): number of items you need to get from the database Returns: result(collection): all elements in the """ try: entries_table = self.db["Entries_Table"] result = entries_table.find().limit(lim) print result[0] return result except errors.ServerSelectionTimeoutError: print('ERROR : No connection could be made because' 'the target machine actively refused it') return None
def __init__(self, server_config, auto_connect=True): """Initialise the client :param server_config: The server configuration :param auto_connect: Whether to automatically connect """ self.server_config = server_config if auto_connect: if ServerSelectionTimeoutError: try: self.connect(server_config) except ServerSelectionTimeoutError as e: logging.warn(e.message) sys.exit() else: self.connect(server_config)
def save_item(self, item): try: self.collection.update({'url': item['url']}, dict(item), upsert=True) except ServerSelectionTimeoutError as e: logging.error('Fail to connect to mongodb. %s', e)
def insert_to_entries_table(self, title, tags, markdown_text): """Insert Data into the Entries_Table in the Database Args: title (str): Title of the Journel Entry tags (list): Tags for the Entry markdown_text(str): Content of the Entry(Markdown text) return: result(bool): True for success or False for failure """ if title == str(""): # print "Error!! Title Can't be Empty" return False entry = {"Title": title, "Date_Created": datetime.now(), "Last_Modified": datetime.now(), "Tags": tags, "MarkdownFile": markdown_text} t = self.db["Entries_Table"] try: if t.insert_one(entry): return True except errors.ServerSelectionTimeoutError: print('ERROR : No connection could be made because' ' the target machine actively refused it') return True
def ensure_mongo(func): @wraps(func) def func_wrapper(*args, **kwargs): client = MongoClient(serverSelectionTimeoutMS=500, connectTimeoutMS=500) try: # The ismaster command is cheap and does not require auth. client.admin.command('ismaster') except (errors.ServerSelectionTimeoutError, errors.AutoReconnect): raise MinionMongoError("Can't connect to mongodb") else: return func(*args, **kwargs) finally: client.close() return func_wrapper
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 crawl_articles(self, numbers): citation_loader = CitationLoader(numbers) entries = citation_loader.get_bibtex() articles = {} for entry in entries: number = entry['ID'] try: article = Article.objects.get(entry_number=number) logger.info('Article [%s] already exists, it will be updated.' % number) except (DoesNotExist, ServerSelectionTimeoutError): article = Article() article.entry_number = number logger.info('Article [%s] is a new article.' % number) article.title = entry['title'] if 'title' in entry else '' article.author = entry['author'] if 'author' in entry else '' article.journal = entry['journal'] if 'journal' in entry else '' article.year = entry['year'] if 'year' in entry else '' article.volume = entry['volume'] if 'volume' in entry else '' article.number = entry['number'] if 'number' in entry else '' article.pages = entry['pages'] if 'pages' in entry else '' article.abstract = entry['abstract'] if 'abstract' in entry else '' article.keyword = entry['keyword'] if 'keyword' in entry else '' article.doi = entry['doi'] if 'doi' in entry else '' article.issn = entry['issn'] if 'issn' in entry else '' article.issue_reference = self.__issue try: article.save() logger.info('Article [%s] saved.' % number) except ServerSelectionTimeoutError: logger.info('Cannot connect to database, Article [%s] will not be saved.' % number) articles[number] = article return articles
def receive_data(self, json_dump_in): # receives data from the plugins result = None current_data = json.loads(json_dump_in) # data may need to be formatted here, prior to sending to database below try: honeypot_database_interface.database.clientInfo.insert_one(current_data).inserted_id except errors.ServerSelectionTimeoutError: self.mail_server.send_mail() except errors.NetworkTimeout: print('Timed out.') return
def select_servers(self, selector, server_selection_timeout=None, address=None): """Return a list of Servers matching selector, or time out. :Parameters: - `selector`: function that takes a list of Servers and returns a subset of them. - `server_selection_timeout` (optional): maximum seconds to wait. If not provided, the default value common.SERVER_SELECTION_TIMEOUT is used. - `address`: optional server address to select. Calls self.open() if needed. Raises exc:`ServerSelectionTimeoutError` after `server_selection_timeout` if no matching servers are found. """ if server_selection_timeout is None: server_timeout = self._settings.server_selection_timeout else: server_timeout = server_selection_timeout with self._lock: self._description.check_compatible() now = _time() end_time = now + server_timeout server_descriptions = self._apply_selector(selector, address) while not server_descriptions: # No suitable servers. if server_timeout == 0 or now > end_time: raise ServerSelectionTimeoutError( self._error_message(selector)) self._ensure_opened() self._request_check_all() # Release the lock and wait for the topology description to # change, or for a timeout. We won't miss any changes that # came after our most recent _apply_selector call, since we've # held the lock until now. self._condition.wait(common.MIN_HEARTBEAT_INTERVAL) self._description.check_compatible() now = _time() server_descriptions = self._apply_selector(selector, address) return [self.get_server_by_address(sd.address) for sd in server_descriptions]
def select_servers(self, selector, server_selection_timeout=None, address=None): """Return a list of Servers matching selector, or time out. :Parameters: - `selector`: function that takes a list of Servers and returns a subset of them. - `server_selection_timeout` (optional): maximum seconds to wait. If not provided, the default value common.SERVER_SELECTION_TIMEOUT is used. - `address`: optional server address to select. Calls self.open() if needed. Raises exc:`ServerSelectionTimeoutError` after `server_selection_timeout` if no matching servers are found. """ if server_selection_timeout is None: server_timeout = self._settings.server_selection_timeout else: server_timeout = server_selection_timeout with self._lock: self._description.check_compatible() now = _time() end_time = now + server_timeout server_descriptions = self._description.apply_selector( selector, address) while not server_descriptions: # No suitable servers. if server_timeout == 0 or now > end_time: raise ServerSelectionTimeoutError( self._error_message(selector)) self._ensure_opened() self._request_check_all() # Release the lock and wait for the topology description to # change, or for a timeout. We won't miss any changes that # came after our most recent apply_selector call, since we've # held the lock until now. self._condition.wait(common.MIN_HEARTBEAT_INTERVAL) self._description.check_compatible() now = _time() server_descriptions = self._description.apply_selector( selector, address) return [self.get_server_by_address(sd.address) for sd in server_descriptions]