21
21
from urllib .parse import unquote_plus
22
22
23
23
from pymongo .common import (
24
+ SRV_SERVICE_NAME ,
24
25
get_validated_options , INTERNAL_URI_OPTION_NAME_MAP ,
25
26
URI_OPTIONS_DEPRECATION_MAP , _CaseInsensitiveDictionary )
26
27
from pymongo .errors import ConfigurationError , InvalidURI
@@ -373,7 +374,7 @@ def _check_options(nodes, options):
373
374
374
375
375
376
def parse_uri (uri , default_port = DEFAULT_PORT , validate = True , warn = False ,
376
- normalize = True , connect_timeout = None ):
377
+ normalize = True , connect_timeout = None , srv_service_name = None ):
377
378
"""Parse and validate a MongoDB URI.
378
379
379
380
Returns a dict of the form::
@@ -405,6 +406,7 @@ def parse_uri(uri, default_port=DEFAULT_PORT, validate=True, warn=False,
405
406
to their internally-used names. Default: ``True``.
406
407
- `connect_timeout` (optional): The maximum time in milliseconds to
407
408
wait for a response from the DNS server.
409
+ - 'srv_service_name` (optional): A custom SRV service name
408
410
409
411
.. versionchanged:: 3.9
410
412
Added the ``normalize`` parameter.
@@ -468,6 +470,9 @@ def parse_uri(uri, default_port=DEFAULT_PORT, validate=True, warn=False,
468
470
if opts :
469
471
options .update (split_options (opts , validate , warn , normalize ))
470
472
473
+ if srv_service_name is None :
474
+ srv_service_name = options .get ("srvServiceName" , SRV_SERVICE_NAME )
475
+
471
476
if '@' in host_part :
472
477
userinfo , _ , hosts = host_part .rpartition ('@' )
473
478
user , passwd = parse_userinfo (userinfo )
@@ -499,7 +504,7 @@ def parse_uri(uri, default_port=DEFAULT_PORT, validate=True, warn=False,
499
504
# Use the connection timeout. connectTimeoutMS passed as a keyword
500
505
# argument overrides the same option passed in the connection string.
501
506
connect_timeout = connect_timeout or options .get ("connectTimeoutMS" )
502
- dns_resolver = _SrvResolver (fqdn , connect_timeout = connect_timeout )
507
+ dns_resolver = _SrvResolver (fqdn , connect_timeout , srv_service_name )
503
508
nodes = dns_resolver .get_hosts ()
504
509
dns_options = dns_resolver .get_options ()
505
510
if dns_options :
@@ -514,6 +519,9 @@ def parse_uri(uri, default_port=DEFAULT_PORT, validate=True, warn=False,
514
519
options [opt ] = val
515
520
if "tls" not in options and "ssl" not in options :
516
521
options ["tls" ] = True if validate else 'true'
522
+ elif not is_srv and options .get ("srvServiceName" ) is not None :
523
+ raise ConfigurationError ("The srvServiceName option is only allowed "
524
+ "with 'mongodb+srv://' URIs" )
517
525
else :
518
526
nodes = split_hosts (hosts , default_port = default_port )
519
527
0 commit comments