LDAP query using Python: always no result

Posted by Grey on Stack Overflow See other posts from Stack Overflow or by Grey
Published on 2010-06-16T20:46:11Z Indexed on 2010/06/16 20:52 UTC
Read the original article Hit count: 221

Filed under:
|
|
|

I am trying to use python to query LDAP server, and it always returns me no result. and anyone help me find what wrong with my python code? it runs fine without excpetions, and it always has no result. i played around with the filter like "cn=partofmyname" but just no luck. thanks for help

import ldap



try:
    l = ldap.open("server")
    l.protocol_version = ldap.VERSION3
    l.set_option(ldap.OPT_REFERRALS, 0)
    output =l.simple_bind("cn=username,cn=Users,dc=domian, dc=net",'password$R')
    print output
except ldap.LDAPError, e:
    print e

baseDN = "DC=rim,DC=net"
searchScope = ldap.SCOPE_SUBTREE
## retrieve all attributes - again adjust to your needs - see documentation for more options
retrieveAttributes = None

Filter = "(&(objectClass=user)(sAMAccountName=myaccount))"

try:
    ldap_result_id = l.search(baseDN, searchScope, Filter, retrieveAttributes)
    print ldap_result_id
    result_set = []
    while 1:
        result_type, result_data = l.result(ldap_result_id, 0)
        if len(result_data) == 0:
            print 'no reslut'
            break
        else:
            for i in range(len(result_set)):
                for entry in result_set[i]:
                    try:
                        name = entry[1]['cn'][0]
                        email = entry[1]['mail'][0]
                        phone = entry[1]['telephonenumber'][0]
                        desc = entry[1]['description'][0]
                        count = count + 1
                        print "%d.\nName: %s\nDescription: %s\nE-mail: %s\nPhone: %s\n" %\
                              (count, name, desc, email, phone)
                    except:
                        pass
                        ## here you don't have to append to a list
                        ## you could do whatever you want with the individual entry
            #if result_type == ldap.RES_SEARCH_ENTRY:
             # result_set.append(result_data)
            #  print result_set
except ldap.LDAPError, e:
    print e
l.unbind()

© Stack Overflow or respective owner

Related posts about python

Related posts about django