[Tutor] python-ldap

Pat Martin python at patm.net
Tue Nov 22 20:08:39 CET 2005


I am new to python and fairly new to programming. I have written some 
ldap scripts in perl and am trying to learn how to do that in python. I 
found some code on the net using the python-ldap module (the unaltered 
code is at the bottom of this email) and have adapted it to my needs, 
the code works fine. I want to learn what some of the things in the code 
are. The two things I don't understand are
ldap.SCOPE_SUBTREE and
ldap.RES_SEARCH_ENTRY
when I print those out I get integers 2 and 100 respectively, I am not 
sure if they change but that is what they start out at. I am figuring 
other modules use similar things (variables?), can someone point me to 
where I can understand what more about what these are.
The help documentation didn't really explain about these.

Thanks
Pat

code:

import ldap

## first you must open a connection to the server
try:
l = ldap.open("127.0.0.1")
## searching doesn't require a bind in LDAP V3.  If you're using LDAP 
v2, set the next line appropriately
## and do a bind as shown in the above example.
# you can also set this to ldap.VERSION2 if you're using a v2 directory
# you should  set the next option to ldap.VERSION2 if you're using a v2 
directory
l.protocol_version = ldap.VERSION3
except ldap.LDAPError, e:
print e
# handle error however you like


## The next lines will also need to be changed to support your search 
requirements and directory
baseDN = "ou=Customers, ou=Sales, o=anydomain.com"
searchScope = ldap.SCOPE_SUBTREE
## retrieve all attributes - again adjust to your needs - see 
documentation for more options
retrieveAttributes = None
searchFilter = "cn=*jack*"

try:
ldap_result_id = l.search(baseDN, searchScope, searchFilter, 
retrieveAttributes)
result_set = []
while 1:
result_type, result_data = l.result(ldap_result_id, 0)
if (result_data == []):
break
else:
## here you don't have to append to a list
## you could do whatever you want with the individual entry
## The appending to list is just for illustration.
if result_type == ldap.RES_SEARCH_ENTRY:
result_set.append(result_data)
print result_set
except ldap.LDAPError, e:
print e


More information about the Tutor mailing list