uri_parser – Tools to parse and validate a MongoDB URI

Tools to parse and validate a MongoDB URI.

  • pymongo.uriparser.parse_host(_entity, default_port=27017)
  • Validates a host string

Returns a 2-tuple of host followed by port where port is default_portif it wasn’t specified in the string.

Parameters:

    • entity: A host or host:port string where host could be a
    • hostname or IP address.
    • default_port: The port number to use when one wasn’t
    • specified in entity.
  • pymongo.uriparser.parse_ipv6_literal_host(_entity, default_port)
  • Validates an IPv6 literal host:port string.

Returns a 2-tuple of IPv6 literal followed by port whereport is default_port if it wasn’t specified in entity.

Parameters:

    • entity: A string that represents an IPv6 literal enclosed
    • in braces (e.g. ‘[::1]’ or ‘[::1]:27017’).
    • default_port: The port number to use when one wasn’t
    • specified in entity.
  • pymongo.uriparser.parse_uri(_uri, default_port=27017, validate=True, warn=False, normalize=True, connect_timeout=None)
  • Parse and validate a MongoDB URI.

Returns a dict of the form:

  1. {
  2. 'nodelist': <list of (host, port) tuples>,
  3. 'username': <username> or None,
  4. 'password': <password> or None,
  5. 'database': <database name> or None,
  6. 'collection': <collection name> or None,
  7. 'options': <dict of MongoDB URI options>,
  8. 'fqdn': <fqdn of the MongoDB+SRV URI> or None
  9. }

If the URI scheme is “mongodb+srv://” DNS SRV and TXT lookups will be doneto build nodelist and options.

Parameters:

  • uri: The MongoDB URI to parse.
  • default_port: The port number to use when one wasn’t specifiedfor a host in the URI.
  • validate (optional): If True (the default), validate andnormalize all options. Default: True.
  • warn (optional): When validating, if True then will warnthe user then ignore any invalid options or values. If False,validation will error when options are unsupported or values areinvalid. Default: False.
  • normalize (optional): If True, convert names of URI optionsto their internally-used names. Default: True.
  • connect_timeout (optional): The maximum time in milliseconds towait for a response from the DNS server.

Changed in version 3.9: Added the normalize parameter.

Changed in version 3.6: Added support for mongodb+srv:// URIs.

Changed in version 3.5: Return the original value of the readPreference MongoDB URI optioninstead of the validated read preference mode.

Changed in version 3.1: warn added so invalid options can be ignored.

  • pymongo.uriparser.parse_userinfo(_userinfo)
  • Validates the format of user information in a MongoDB URI.Reserved characters like ‘:’, ‘/’, ‘+’ and ‘@’ must be escapedfollowing RFC 3986.

Returns a 2-tuple containing the unescaped username followedby the unescaped password.

Paramaters:

  • userinfo: A string of the form :

Changed in version 2.2: Now uses urllib.unquote_plus so + characters must be escaped.

  • pymongo.uriparser.split_hosts(_hosts, default_port=27017)
  • Takes a string of the form host1[:port],host2[:port]… andsplits it into (host, port) tuples. If [:port] isn’t present thedefault_port is used.

Returns a set of 2-tuples containing the host name (or IP) followed byport number.

Parameters:

  • hosts: A string of the form host1[:port],host2[:port],…
  • default_port: The port number to use when one wasn’t specifiedfor a host.
  • pymongo.uriparser.split_options(_opts, validate=True, warn=False, normalize=True)
  • Takes the options portion of a MongoDB URI, validates each optionand returns the options in a dictionary.

Parameters:

  • opt: A string representing MongoDB URI options.
  • validate: If True (the default), validate and normalize alloptions.
  • warn: If False (the default), suppress all warnings raisedduring validation of options.
  • normalize: If True (the default), renames all options to theirinternally-used names.
  • pymongo.uriparser.validate_options(_opts, warn=False)
  • Validates and normalizes options passed in a MongoDB URI.

Returns a new dictionary of validated and normalized options. If warn isFalse then errors will be thrown for invalid options, otherwise they willbe ignored and a warning will be issued.

Parameters:

  • opts: A dict of MongoDB URI options.
  • warn (optional): If True then warnings will be logged andinvalid options will be ignored. Otherwise invalid options willcause errors.