[docs]defparse_uri(uri):""" :param uri: example would be "mongodb://user:pencil@localhost:27017/admin?retryWrites=true&w=majority" :return: dict of the form { 'nodelist': <list of (host, port) tuples>, 'username': <username> or None, 'password': <password> or None, 'database': <database name> or None, 'collection': <collection name> or None, 'options': <dict of MongoDB URI options>, 'fqdn': <fqdn of the MongoDB+SRV URI> or None } """parsed=urlparse(uri)username,password=None,Noneif"@"inparsed.netloc:user_password,host_port=parsed.netloc.split("@")username,password=user_password.split(":")else:host_port=parsed.netlocnodes=host_port.split(":")database=parsed.pathifparsed.path!="/"else"admin"resp={"nodelist":nodes,"username":usernameifusernameelseNone,"password":passwordifpasswordelseNone,"database":database,"collection":None,}returnresp