Search code examples
pythonpython-3.xldappython-ldapldap3

Parsing DN, attributes from LDAP URL in Python


I have an URL "ldap://ldap.infonotary.com/dc=identity-ca,dc=infonotary,dc=com"

I wanted to get the LDAP DN and attributes from this URL using ldap3 library in python3. If I pass the whole URL to a class, I should be able to get DN, attributes from its functions. Also I should get hostname, extensions, port.


Solution

  • Function ldap3.utils.uri.parse_uri() is your friend which returns a dictionary with the URL components:

    >>> import ldap3.utils.uri    
    >>> ldap3.utils.uri.parse_uri('ldap://ldap.infonotary.com/dc=identity-ca%2cdc=infonotary%2cdc=com')['base']
    'dc=identity-ca,dc=infonotary,dc=com'