本文整理汇总了Python中pymongo.uri_parser.parse_uri函数的典型用法代码示例。如果您正苦于以下问题:Python parse_uri函数的具体用法?Python parse_uri怎么用?Python parse_uri使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了parse_uri函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, db_config):
self.config = db_config
self.force_quit = False
# sanitize the options
if self.config["target_collections"] is not None:
self.config["target_collections"] = set(
[coll.strip() for coll in self.config["target_collections"]])
if 'auto_config' in self.config and self.config['auto_config'] is True:
if 'auth_db' not in self.config['auto_config_options']:
try:
self.config['auto_config_options']['auth_db'] = self.config['auth_db']
except Exception:
pass
if 'user' not in self.config['auto_config_options']:
try:
self.config['auto_config_options']['user'] = self.config['user']
except Exception:
pass
if 'password' not in self.config['auto_config_options']:
try:
self.config['auto_config_options']['password'] = self.config['password']
except Exception:
pass
self.get_topology(self.config['auto_config_options'])
oplog_servers = self.build_oplog_servers(self.config['auto_config_options'])
profiler_servers = self.build_profiler_servers(self.config['auto_config_options'])
else:
oplog_servers = self.config["oplog_servers"]
profiler_servers = self.config["profiler_servers"]
if len(oplog_servers) < 1 or len(profiler_servers) < 1:
utils.log.error("Detected either no profile or oplog servers, bailing")
sys.exit(1)
self.oplog_clients = {}
for index, server in enumerate(oplog_servers):
mongodb_uri = server['mongodb_uri']
nodelist = uri_parser.parse_uri(mongodb_uri)["nodelist"]
server_string = "%s:%s" % (nodelist[0][0], nodelist[0][1])
self.oplog_clients[server_string] = self.connect_mongo(server)
utils.LOG.info("oplog server %d: %s", index, self.sanatize_server(server))
# create a mongo client for each profiler server
self.profiler_clients = {}
for index, server in enumerate(profiler_servers):
mongodb_uri = server['mongodb_uri']
nodelist = uri_parser.parse_uri(mongodb_uri)["nodelist"]
server_string = "%s:%s" % (nodelist[0][0], nodelist[0][1])
self.profiler_clients[server_string] = self.connect_mongo(server)
utils.LOG.info("profiling server %d: %s", index, self.sanatize_server(server))
示例2: run_test
def run_test(self):
if not _HAVE_DNSPYTHON:
raise unittest.SkipTest("DNS tests require the dnspython module")
uri = test_case['uri']
seeds = test_case['seeds']
hosts = test_case['hosts']
options = test_case.get('options')
if seeds:
seeds = split_hosts(','.join(seeds))
if hosts:
hosts = frozenset(split_hosts(','.join(hosts)))
if options:
for key, value in options.items():
# Convert numbers / booleans to strings for comparison
if isinstance(value, bool):
options[key] = 'true' if value else 'false'
elif isinstance(value, (int, float)):
options[key] = str(value)
if seeds:
result = parse_uri(uri, validate=False)
self.assertEqual(sorted(result['nodelist']), sorted(seeds))
if options:
opts = result['options']
if 'readpreferencetags' in opts:
rpts = validate_read_preference_tags(
'readPreferenceTags', opts.pop('readpreferencetags'))
opts['readPreferenceTags'] = rpts
self.assertEqual(result['options'], options)
hostname = next(iter(client_context.client.nodes))[0]
# The replica set members must be configured as 'localhost'.
if hostname == 'localhost':
copts = client_context.default_client_options.copy()
if client_context.ssl is True:
# Our test certs don't support the SRV hosts used in these tests.
copts['ssl_match_hostname'] = False
client = MongoClient(uri, **copts)
# Force server selection
client.admin.command('ismaster')
wait_until(
lambda: hosts == client.nodes,
'match test hosts to client nodes')
else:
try:
parse_uri(uri)
except (ConfigurationError, ValueError):
pass
else:
self.fail("failed to raise an exception")
示例3: __init__
def __init__(self, db_config):
self.config = db_config
self.force_quit = False
# Sanitize the config options
self.config["target_collections"] = set(
[coll.strip() for coll in self.config.get("target_collections", [])])
if self.config.get('auto_config'):
if 'auto_config_options' not in self.config:
self.config['auto_config_options'] = {}
if 'auth_db' not in self.config['auto_config_options'] and 'auth_db' in self.config:
self.config['auto_config_options']['auth_db'] = self.config['auth_db']
if 'user' not in self.config['auto_config_options'] and 'user' in self.config:
self.config['auto_config_options']['user'] = self.config['user']
if 'password' not in self.config['auto_config_options'] and 'password' in self.config:
self.config['auto_config_options']['password'] = self.config['password']
self.get_topology(self.config['auto_config_options'])
oplog_servers = self.build_oplog_servers(self.config['auto_config_options'])
profiler_servers = self.build_profiler_servers(self.config['auto_config_options'])
else:
oplog_servers = self.config["oplog_servers"]
profiler_servers = self.config["profiler_servers"]
if len(oplog_servers) < 1 or len(profiler_servers) < 1:
utils.log.error("Detected either no profile or oplog servers, bailing")
sys.exit(1)
# Connect to each MongoDB server that we want to get the oplog data from
self.oplog_clients = {}
for index, server in enumerate(oplog_servers):
mongodb_uri = server['mongodb_uri']
nodelist = uri_parser.parse_uri(mongodb_uri)["nodelist"]
server_string = "%s:%s" % (nodelist[0][0], nodelist[0][1])
self.oplog_clients[server_string] = self.connect_mongo(server)
utils.LOG.info("oplog server %d: %s", index, self.sanitize_server(server))
# Connect to each MongoDB server that we want to get the profile data from
self.profiler_clients = {}
for index, server in enumerate(profiler_servers):
mongodb_uri = server['mongodb_uri']
nodelist = uri_parser.parse_uri(mongodb_uri)["nodelist"]
server_string = "%s:%s" % (nodelist[0][0], nodelist[0][1])
self.profiler_clients[server_string] = self.connect_mongo(server)
utils.LOG.info("profiling server %d: %s", index, self.sanitize_server(server))
utils.LOG.info('Successfully connected to %d oplog server(s) and %d profiler server(s)', len(self.oplog_clients), len(self.profiler_clients))
示例4: init_db
def init_db(mongo_uri):
global _conn
global _db
dbname = uri_parser.parse_uri(mongo_uri)["database"]
_conn = MongoClient(host=mongo_uri)
_conn.register([model.Instance])
_db = _conn[dbname]
示例5: get_mongo_uri
def get_mongo_uri(dasconfig):
""" read dasconfig and return mongodb host and port (as dict) """
# use debug=False to suppress printouts about DAS config
uri = dasconfig['mongodb']['dburi'][0]
parsed_uri = parse_uri(uri)
host, port = parsed_uri['nodelist'][0]
return dict(mongo_host=host, mongo_port=port)
示例6: __init__
def __init__(self, location, params, mongodb=None):
"""
@type mongodb: инстанс подключения к монго
"""
self.collection_name = params['collection']
options = params.get('OPTIONS', {})
self.write_concern = options.get('WRITE_CONCERN', 1)
self.json_serializeable_values = options.get(
'VALUES_ARE_JSON_SERIALIZEABLE', True)
strategy = options.get('STRATEGY', 'NEAREST')
assert self.write_concern > -1
assert isinstance(self.collection_name, basestring)
if not location.startswith('mongodb://'):
raise ImproperlyConfigured('connection to mongo should start with mongodb://')
database = uri_parser.parse_uri(location)['database']
if not database:
raise ImproperlyConfigured('Specify DB like that mongodb://hosts/database_name')
self.mongodb = mongodb or MongoDBWrapper(
hosts=location,
strategy=strategy,
replica_set=options['replica_set'],
database_name=database
)
self.logger = logging.getLogger('mongo_requests')
super(MongoDBCache, self).__init__(params)
self._ensure_ttl_collection()
示例7: test_parse_uri_unicode
def test_parse_uri_unicode(self):
# Ensure parsing a unicode returns option names that can be passed
# as kwargs. In Python 2.4, keyword argument names must be ASCII.
# In all Pythons, str is the type of valid keyword arg names.
res = parse_uri(unicode("mongodb://localhost/?fsync=true"))
for key in res['options']:
self.assertTrue(isinstance(key, str))
示例8: __init__
def __init__(self, uri="mongodb://127.0.0.1:27017", pool_size=1, ssl_context_factory=None, **kwargs):
assert isinstance(uri, basestring)
assert isinstance(pool_size, int)
assert pool_size >= 1
if not uri.startswith("mongodb://"):
uri = "mongodb://" + uri
self.__uri = parse_uri(uri)
wc_options = self.__uri['options'].copy()
wc_options.update(kwargs)
wc_options = dict((k, v) for k, v in wc_options.items() if k in self.__wc_possible_options)
self.__write_concern = WriteConcern(**wc_options)
self.__pool_size = pool_size
self.__pool = [_Connection(self, self.__uri, i) for i in range(pool_size)]
if self.__uri['database'] and self.__uri['username'] and self.__uri['password']:
self.authenticate(self.__uri['database'], self.__uri['username'],
self.__uri['password'],
self.__uri['options'].get('authmechanism', 'DEFAULT'))
host, port = self.__uri['nodelist'][0]
for factory in self.__pool:
if ssl_context_factory:
factory.connector = reactor.connectSSL(host, port, factory, ssl_context_factory)
else:
factory.connector = reactor.connectTCP(host, port, factory)
示例9: get_collection
def get_collection(uri):
uri_info = uri_parser.parse_uri(uri)
col = uri_info["collection"]
database = get_database(uri)
return database[col]
示例10: __init__
def __init__(self, db_url, collection_name):
client = MongoClient(db_url)
parsed = uri_parser.parse_uri(db_url)
if 'database' not in parsed:
raise ValueError('database not in uri: {}', db_url)
db = client[parsed['database']]
self.c = db[collection_name]
示例11: __init__
def __init__(self, db_config):
self.config = db_config
self.force_quit = False
# sanitize the options
if self.config["target_collections"] is not None:
self.config["target_collections"] = set(
[coll.strip() for coll in self.config["target_collections"]])
oplog_server = self.config["oplog_server"]
profiler_servers = self.config["profiler_servers"]
mongodb_uri = oplog_server["mongodb_uri"]
self.oplog_client = MongoClient(mongodb_uri)
# create a mongo client for each profiler server
self.profiler_clients = {}
for index, server in enumerate(profiler_servers):
mongodb_uri = server['mongodb_uri']
nodelist = uri_parser.parse_uri(mongodb_uri)["nodelist"]
server_string = "%s:%s" % (nodelist[0][0], nodelist[0][1])
self.profiler_clients[server_string] = MongoClient(mongodb_uri,
slaveOk=True)
utils.LOG.info("profiling server %d: %s", index, str(server))
utils.LOG.info("oplog server: %s", str(oplog_server))
示例12: _get_db
def _get_db():
"""
Returns the connection to the database using the settings.
This function should not be called outside of this file.
Use db instead.
"""
from .settings import settings
mongo = settings.MONGODB
if 'URI' in mongo and mongo['URI']:
uri = mongo['URI']
else:
uri = 'mongodb://'
if all(mongo.get(key) for key in ('USERNAME', 'PASSWORD')):
uri += '{0}:{1}@'.format(mongo['USERNAME'], mongo['PASSWORD'])
if 'HOSTS' in mongo and mongo['HOSTS']:
uri += ','.join(
'{0}:{1}'.format(host, port)
for (host, port) in zip(mongo['HOSTS'], mongo['PORTS']),
)
else:
uri += '{0}:{1}'.format(mongo['HOST'], mongo.get('PORT', 27017))
uri += '/' + mongo['DATABASE']
if 'OPTIONS' in mongo and mongo['OPTIONS']:
uri += '?{0}'.format('&'.join(mongo['OPTIONS']))
return MongoClient(uri)[parse_uri(uri)['database']]
示例13: _create_connection
def _create_connection(conn_settings, testing=False):
# Handle multiple connections recursively
if isinstance(conn_settings, list):
connections = {}
for conn in conn_settings:
connections[conn.get('alias')] = _create_connection(conn, testing)
return connections
# Ugly dict comprehention in order to support python 2.6
conn = dict((k.lower(), v) for k, v in conn_settings.items() if v is not None)
if 'replicaset' in conn:
conn['replicaSet'] = conn.pop('replicaset')
if (StrictVersion(mongoengine.__version__) >= StrictVersion('0.10.6') and
testing and conn.get('host', '').startswith('mongomock://')):
pass
# Handle uri style connections
elif "://" in conn.get('host', ''):
uri_dict = uri_parser.parse_uri(conn['host'])
conn['db'] = uri_dict['database']
if conn['db'] is None:
raise ValueError('Mongo host URI must contain database name')
return mongoengine.connect(conn.pop('db', 'test'), **conn)
示例14: connect
def connect(mongodb_uri, alias=DEFAULT_CONNECTION_ALIAS):
"""Register a connection to MongoDB, optionally providing a name for it.
:parameters:
- `mongodb_uri`: A MongoDB connection string. Any options may be passed
within the string that are supported by PyMongo. `mongodb_uri` must
specify a database, which will be used by any
:class:`~pymodm.MongoModel` that uses this connection.
- `alias`: An optional name for this connection, backed by a
:class:`~pymongo.mongo_client.MongoClient` instance that is cached under
this name. You can specify what connection a MongoModel uses by
specifying the connection's alias via the `connection_alias` attribute
inside their `Meta` class. Switching connections is also possible using
the :class:`~pymodm.context_managers.switch_connection` context
manager. Note that calling `connect()` multiple times with the same
alias will replace any previous connections.
"""
# Make sure the database is provided.
parsed_uri = uri_parser.parse_uri(mongodb_uri)
if not parsed_uri.get('database'):
raise ValueError('Connection must specify a database.')
_CONNECTIONS[alias] = ConnectionInfo(
parsed_uri=parsed_uri,
conn_string=mongodb_uri,
database=MongoClient(mongodb_uri)[parsed_uri['database']])
示例15: collect
def collect(self):
server = self.config.get('server')
if server == None:
self.error("Missing 'server' in mongo config")
return
parsed = uri_parser.parse_uri(server)
username = parsed.get('username')
password = parsed.get('password')
db_name = parsed.get('database')
slowms = self.config.get('slowms', 25)
if not db_name:
self.log.info('No MongoDB database found in URI. Defaulting to admin.')
db_name = 'admin'
do_auth = True
if username is None or password is None:
self.log.debug("Mongo: cannot extract username and password from config %s" % server)
do_auth = False
try:
self.conn = pymongo.Connection(server, network_timeout=self.DEFAULT_TIMEOUT)
except Exception, e:
self.error(e)
return