简体   繁体   中英

python ldap3 function for massive ldap searches

I'm new to ldap3 library and I'm trying to build a function that need to search ldap for 55.000 uids and retrieve their mail attribute.

Here is what it looks like :

def ldapsearch(i):
server = Server('myserverurl:389')
try:
    with Connection(server) as conn:
        conn.search('ou=people,dc=xxx,dc=fr', '(&(objectclass=person)(uid='+i+'))', attributes=['mail', 'cn', 'uid'])
    entry = conn.entries[0]        
except:
    return'error search operation'
else:
    if (len(entry)!=0):
        return entry['mail']
    else:
        return'error uid not found'

But I can't get it working... Any idea ?

Thx in advance for your help, Nicolas

I found the answer :

    from ldap3 import Server, Connection, ALL    
    def ldapsearch(i):
        try:
            conn.search('ou=people,dc=xxxx,dc=fr', '(&(objectclass=person)(uid='+i+'))', attributes=['mail', 'cn', 'uid'])
            return conn.entries[0]['mail']        
        except:
            return'uid not found'

server = Server('myldapserver_url:389')
conn = Connection(server, auto_bind=True)

ldapsearch('myUid')

;-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM