Welcome, guest | Sign In | My Account | Store | Cart

The ldap library at http://python-ldap.sourceforge.net wraps the Openldap C api. It can talk to various versions of ldap servers not just the Openldap servers. Note the use of the '_s' methods like search_s which all are synchronous. Here are some simple examples showing one how to use the library.

Python, 34 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
try:
    path='cn=people,ou=office,o=company'
    l=ldap.open('hostname')
    #set which protocol if you do not like the default
    l.protocol_version = ldap.VERSION2
    l.simple_bind('cn=root,ou=office,o=company','password')

    #search for surnames beginning with a
    #available options for how deep a search you want.
    #LDAP_SCOPE_BASE, LDAP_SCOPE_ONELEVEL,LDAP_SCOPE_SUBTREE,
    a=l.search_s(path,ldap.SCOPE_SUBTREE,'sn='+'a*')

    #delete fred
    l.delete_s('cn=fred,'+path)

    #add barney
    #note: objectclass is dependant on the ldap server
    user_info = {'uid':'barney123',
                'givenname':'Barney',
                'cn':'barney123',
                'sn':'Smith',
                'telephonenumber':'123-4567',
                'facsimiletelephonenumber':'987-6543',
                'objectclass':('Remote-Address','person', 'Top'),
                'physicaldeliveryofficename':'Services',
                'mail':'fred123@company.com',
                'title':'programmer',
                }

    id='cn=barney,'+path
    attributes=[ (k,v) for k,v in user_info.items() ]
    l.add_s(id,attributes)    
except ldap.LDAPError, error:
    print 'problem with ldap',error
     

Fundamentally with ldap (as with many other things), you can search,create, and delete. The examples show just that, for more extensive documentation consult http://python-ldap.sourceforge.net/docs.shtml.

You can reach me at pyguy2 on yahoo.com.

3 comments

Calvin Hendryx-Parker 19 years, 7 months ago  # | flag

Great example, but are there any good async examples? I have used this lib a bit and it is pretty easy, but I haven't tried to use it asyncronously. I looked through the samples that came with the code, but didn't find a good complete example of using the async methods.

Maybe you can post a quick one?

michael 19 years, 5 months ago  # | flag

ldap.async. Watch out the python-ldap docs for the sub-module ldap.async which is meant for stream processing of large result sets. The docs solely contain a single example. But that should be sufficient to get the idea.

michael 19 years, 5 months ago  # | flag

Do not use LDAPv2. You should not mess with l.protocol_version unless you really know what you're doing!!!

LDAPv2 is a deprecated protocol version (RFCs marked as historic). LDAPv3 is what most LDAP servers implement today.

Therefore python-ldap's default for l.protocol_version is ldap.VERSION3 which sets LDAPv3 for binds.

Created by John Nielsen on Fri, 3 Sep 2004 (PSF)
Python recipes (4591)
John Nielsen's recipes (36)

Required Modules

  • (none specified)

Other Information and Tasks