本文整理汇总了Python中pymongo.connection.Connection.close方法的典型用法代码示例。如果您正苦于以下问题:Python Connection.close方法的具体用法?Python Connection.close怎么用?Python Connection.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pymongo.connection.Connection
的用法示例。
在下文中一共展示了Connection.close方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: check_conn
# 需要导入模块: from pymongo.connection import Connection [as 别名]
# 或者: from pymongo.connection.Connection import close [as 别名]
def check_conn (ip = "localhost", port = 27017):
try:
port = int(port)
conn = Connection (host = ip, port = port)
except Exception as E:
logging.error (E)
return False
else:
conn.close ()
return True
示例2: MongoDBStore
# 需要导入模块: from pymongo.connection import Connection [as 别名]
# 或者: from pymongo.connection.Connection import close [as 别名]
class MongoDBStore(SimpleStore):
'''MongoDB-based object storage frontend.'''
init = 'mongodb://'
def __init__(self, engine, **kw):
super(MongoDBStore, self).__init__(engine, **kw)
spliturl = urlsplit(engine)
_, dbpath, self._colpath = spliturl.path.split('/')
self._conn = Connection(host=spliturl.hostname, port=spliturl.port)
self._db = getattr(self._conn, dbpath)
self._store = getattr(self._db, self._colpath)
self._store.ensure_index('key', unique=True)
def __getitem__(self, key):
try:
return self.loads(self._store.find_one(dict(key=key))['value'], key)
except TypeError:
raise KeyError(key)
def __setitem__(self, key, value):
self._store.save(dict(key=key, value=Binary(self.dumps(value))))
def __delitem__(self, key):
self._store.remove(dict(key=key))
def __len__(self):
return self._store.count()
def __iter__(self):
for key in self._store.find(
dict(key={'$exists': True}), fields=['key'],
):
yield key['key']
def close(self):
self._conn.close()
def clear(self):
self._store.drop()
self._store = getattr(self._db, self._colpath)
def items(self):
for key in self._store.find({'key': {'$exists': True}}):
yield (key['key'], key['value'])
def values(self):
loads = self.loads
for value in self._store.distinct('key'):
yield loads(value['value'])
示例3: init_messaging_agents
# 需要导入模块: from pymongo.connection import Connection [as 别名]
# 或者: from pymongo.connection.Connection import close [as 别名]
def init_messaging_agents():
mongodb = None
while not mongodb:
try:
mongodb = Connection(options.mongohost, options.mongoport)
except Exception as ex:
logging.error(ex)
# wait 5 secs to reconnect
time.sleep(5)
masterdb = mongodb[options.masterdb]
apps = masterdb.applications.find()
httpconns = {}
apnsconns = {}
for app in apps:
''' APNs setup '''
apnsconns[app['shortname']] = []
conns = int(app['connections'])
if conns < 1:
conns = 1
if 'environment' not in app:
app['environment'] = 'sandbox'
if 'certfile' in app and 'keyfile' in app and 'shortname' in app:
for instanceid in range(0, conns):
try:
apn = APNClient(app['environment'], app['certfile'], app['keyfile'], app['shortname'], instanceid)
except Exception as ex:
logging.error(ex)
continue
apnsconns[app['shortname']].append(apn)
''' GCMClient setup '''
httpconns[app['shortname']] = []
if 'gcmprojectnumber' in app and 'gcmapikey' in app and 'shortname' in app:
try:
http = GCMClient(app['gcmprojectnumber'], app['gcmapikey'], app['shortname'], 0)
except Exception as ex:
logging.error(ex)
continue
httpconns[app['shortname']].append(http)
mongodb.close()
return apnsconns, httpconns
示例4: init_messaging_agents
# 需要导入模块: from pymongo.connection import Connection [as 别名]
# 或者: from pymongo.connection.Connection import close [as 别名]
def init_messaging_agents():
services = {"gcm": {}, "wns": {}, "apns": {}, "mpns": {}, "sms": {}}
mongodb = None
while not mongodb:
try:
mongodb = Connection(options.mongohost, options.mongoport)
except Exception as ex:
_logger.error(ex)
masterdb = mongodb[options.masterdb]
apps = masterdb.applications.find()
for app in apps:
""" APNs setup """
services["apns"][app["shortname"]] = []
conns = int(app["connections"])
if conns < 1:
conns = 1
if "environment" not in app:
app["environment"] = "sandbox"
if file_exists(app.get("certfile", False)) and file_exists(app.get("keyfile", False)) and "shortname" in app:
if app.get("enableapns", False):
for instanceid in range(0, conns):
try:
apn = APNClient(
app["environment"], app["certfile"], app["keyfile"], app["shortname"], instanceid
)
except Exception as ex:
_logger.error(ex)
continue
services["apns"][app["shortname"]].append(apn)
""" GCMClient setup """
services["gcm"][app["shortname"]] = []
if "gcmprojectnumber" in app and "gcmapikey" in app and "shortname" in app:
try:
http = GCMClient(app["gcmprojectnumber"], app["gcmapikey"], app["shortname"], 0)
except Exception as ex:
_logger.error(ex)
continue
services["gcm"][app["shortname"]].append(http)
""" WNS setup """
services["wns"][app["shortname"]] = []
if "wnsclientid" in app and "wnsclientsecret" in app and "shortname" in app:
try:
wns = WNSClient(masterdb, app, 0)
except Exception as ex:
_logger.error(ex)
continue
services["wns"][app["shortname"]].append(wns)
""" MPNS setup """
services["mpns"][app["shortname"]] = []
try:
mpns = MPNSClient(masterdb, app, 0)
except Exception as ex:
_logger.error(ex)
continue
services["mpns"][app["shortname"]].append(mpns)
""" clickatell """
services["sms"][app["shortname"]] = []
try:
sms = ClickatellClient(masterdb, app, 0)
except Exception as ex:
_logger.error(ex)
continue
services["sms"][app["shortname"]].append(sms)
mongodb.close()
return services
示例5: Database
# 需要导入模块: from pymongo.connection import Connection [as 别名]
# 或者: from pymongo.connection.Connection import close [as 别名]
class Database():
def __init__(self, db_name, drop_dbs=True):
self.connection=Connection()
self.database = self.connection[db_name]
self.pages = self.database.pages
#self.pages.ensure_index( "id", pymongo.ASCENDING, unique=True)
self.urls = self.database.urls
self.url_descriptions = self.database.url_describtion
self.clickables = self.database.clickables
self.delta_pages = self.database.delta_pages
self.forms = self.database.forms
self.users = self.database.users
self.clusters = self.database.clusters
self.attack = self.database.attack
self.async_requests = self.database.asyncrequests
self.async_request_structure = self.database.asyncrequeststructure
self._per_session_url_counter = 0
if drop_dbs:
self.pages.drop() #Clear database
self.urls.drop() #Clear database
self.url_descriptions.drop()
self.delta_pages.drop()
self.clickables.drop()
self.forms.drop()
self.users.drop()
self.clusters.drop()
self.attack.drop()
self.async_requests.drop()
self.async_request_structure.drop()
self.urls.ensure_index("url", pymongo.ASCENDING, unique=True)
#self.url_descriptions.ensure_index("hash", pymongo.ASCENDING, unique=True)
def __del__(self):
self.connection.close()
def prepare_for_new_crawling(self):
self._per_session_url_counter = 0
def get_user_to_username(self, username):
search_doc = {'username': username}
user = self.users.find_one(search_doc)
if user is None:
return None
return user['session']
def insert_user_into_db(self, user):
num_of_users = self.users.count()
user_id = num_of_users + 1
doc = self._user_to_doc(user)
doc['_id'] = user_id
if user.login_data is not None and user.url_with_login_form is not None:
doc['url_with_login_form'] = user.url_with_login_form
doc['login_data'] = user.login_data
self.users.save(doc)
def _user_to_doc(self, user):
doc = {}
doc['user_level'] = user.user_level
doc['username'] = user.username
doc['session'] = user.session
return doc
def insert_url_into_db(self, current_session, url, is_redirected_url = False):
"""
:param current_session:
:param url:
:param is_redirected_url:
:return: True if url is insert, if url exists False
"""
if self.urls.find({"url": url.toString(), "session": current_session}).count() > 0 or self.urls.find({"redirected_to": url.toString(), "session": current_session}).count() > 0:
return False
document = self._url_to_doc_without_abstract_url(url)
document['session'] = current_session
document["url_counter"] = self._per_session_url_counter
self._per_session_url_counter += 1
self.urls.save(document)
return True
def _url_to_doc_without_abstract_url(self, url):
doc = {}
doc["url"] = url.complete_url
#doc["abstract_url"] = url.abstract_url
doc["url_hash"] = url.url_hash
doc["page_id"] = None
doc["visited"] = False
doc["response_code"] = None
doc['redirected_to'] = None
doc['depth_of_finding'] = url.depth_of_finding
return doc
def get_next_url_for_crawling(self, current_session):
urls = self.urls.find({"session": current_session, "visited": False}).sort([('url_counter', pymongo.ASCENDING)]).limit(1)
if urls is None or urls.count() == 0:
return None
else:
#.........这里部分代码省略.........
示例6: init_messaging_agents
# 需要导入模块: from pymongo.connection import Connection [as 别名]
# 或者: from pymongo.connection.Connection import close [as 别名]
def init_messaging_agents():
services = {
'gcm': {},
'wns': {},
'apns': {},
'mpns': {},
'sms': {},
}
mongodb = None
while not mongodb:
try:
mongodb = Connection(options.mongohost, options.mongoport)
except Exception as ex:
logging.error(ex)
masterdb = mongodb[options.masterdb]
apps = masterdb.applications.find()
for app in apps:
''' APNs setup '''
services['apns'][app['shortname']] = []
conns = int(app['connections'])
if conns < 1:
conns = 1
if 'environment' not in app:
app['environment'] = 'sandbox'
if file_exists(app.get('certfile', False)) and file_exists(app.get('keyfile', False)) and 'shortname' in app:
if app.get('enableapns', False):
for instanceid in range(0, conns):
try:
apn = APNClient(app['environment'], app['certfile'], app['keyfile'], app['shortname'], instanceid)
except Exception as ex:
logging.error(ex)
continue
services['apns'][app['shortname']].append(apn)
''' GCMClient setup '''
services['gcm'][app['shortname']] = []
if 'gcmprojectnumber' in app and 'gcmapikey' in app and 'shortname' in app:
try:
http = GCMClient(app['gcmprojectnumber'], app['gcmapikey'], app['shortname'], 0)
except Exception as ex:
logging.error(ex)
continue
services['gcm'][app['shortname']].append(http)
''' WNS setup '''
services['wns'][app['shortname']] = []
if 'wnsclientid' in app and 'wnsclientsecret' in app and 'shortname' in app:
try:
wns = WNSClient(masterdb, app, 0)
except Exception as ex:
logging.error(ex)
continue
services['wns'][app['shortname']].append(wns)
''' MPNS setup '''
services['mpns'][app['shortname']] = []
try:
mpns = MPNSClient(masterdb, app, 0)
except Exception as ex:
logging.error(ex)
continue
services['mpns'][app['shortname']].append(mpns)
''' clickatell '''
services['sms'][app['shortname']] = []
try:
sms = ClickatellClient(masterdb, app, 0)
except Exception as ex:
logging.error(ex)
continue
services['sms'][app['shortname']].append(sms)
mongodb.close()
return services