Python3-ldap KeyError: 'attributes'
Question: Using Python3.4 with the `python3-ldap` module loaded. Using the code:

    
    
    from ldap3 import Server, Connection, SEARCH_SCOPE_WHOLE_SUBTREE, AUTO_BIND_NO_TLS #For title queires into LDAP
    
    def GetTitle(u):
        print(u)
        t=[]
    
        server = Server(DomanCtrlr)
        c = Connection(server,
                       auto_bind=AUTO_BIND_NO_TLS,
                       read_only=True,
                       check_names=True,
                       user = user,
                       password= password)
    
        c.search(search_base = 'dc=corp,dc=weyer,dc=pri',
                 search_filter = '(&(samAccountName=' + u + '))',
                 search_scope = SEARCH_SCOPE_WHOLE_SUBTREE,
                 attributes = ['title'],
                 paged_size = 5)
    
        for entry in c.response:
            print(entry['attributes']['title'])
            t = entry['attributes']['title']
            print(u, " : ", t)
    
    users = ['user1', 'notAuser', 'user2']
    
    for u in users:
        GetTitle(u)
    

I expect to get an error for notAuser, but instead I get this output:

    
    
    user1
    ['CONTROL ROOM OPERATOR']
    user1  :  ['CONTROL ROOM OPERATOR']
    Traceback (most recent call last):
      File "C:\Users\olsonma\Documents\ThreatMatrix_PY\LDAPTest.py", line 28, in <module>
        GetTitle(u)
      File "C:\Users\olsonma\Documents\ThreatMatrix_PY\LDAPTest.py", line 17, in GetTitle
        print(entry['attributes']['title'])
    KeyError: 'attributes'
    

Since the line obviously executed I am not clear how the error is happening.

I have found a lot of articles for the old `python-ldap` error similar to
this, but the option to fix it for python-ldap does not seen to be an option
for `python3-ldap`. Does anyone know 1.) why this happening? 2.) how to make
it stop?


Answer: Prints have been removed from code since I have found the issue.

    
    
    from ldap3 import Server, Connection, SEARCH_SCOPE_WHOLE_SUBTREE, AUTO_BIND_NO_TLS #For title queires into LDAP
    
    def GetTitle(u):
    
        t=''
    
        server = Server(DomainCtrlr)
        c = Connection(server,
                       auto_bind=AUTO_BIND_NO_TLS,
                       read_only=True,
                       check_names=True,
                       user = user,
                       password= password)
    
        c.search(search_base = 'dc=corp,dc=weyer,dc=pri',
                 search_filter = '(&(samAccountName=' + u + '))',
                 search_scope = SEARCH_SCOPE_WHOLE_SUBTREE,
                 attributes = ['title'],
                 paged_size = 5)
    
       if len(c.response) > 1:
            for entry in c.response:
                t = entry['attributes']['title']
                return t
    
    users = ['lafrenh', 'userid', 'garlockb']
    
    for u in users:
        title = GetTitle(u)
        print(title)
    

