LDAP server in Python

Michael Ströder michael at stroeder.com
Tue Mar 18 08:20:46 EST 2003


Ganesan R wrote:
> 
> ------
> import ldap
> l = ldap.init("yourhostname")

Note that using ldap.initialize('<LDAP URL>') is preferred since it also 
supports connecting LDAP over SSL (ldaps://..) or LDAP over Unix domain 
socket (ldapi://..).

> l.protocol_vesrion = 3                     # not mandatory
                ^^^
A typo - should be "protocol_version". It's highly recommended to set the 
protocol version since this might switch some features in a very strict LDAP 
front-end of a X.500 server (e.g. character set used).

> res = l.search_s("o=oraclenames", ldap.SCOPE_BASE, "objectclass=*")
> for dn, entry in res:
>     print "dn: ", dn
>     print "entry: ", entry
> ------
> 
> That's all. The dn is not relevant in this example (it will be
> "o=oraclenames"). The entry is a dictionary of key, value pairs. Since
> LDAP allows multiple values for attributes, the "value" may be a list.

The "value" is always a list even if there's only one attribute value.

Another simple example (with weird scandinavian UTF-8 chars in attribute 
attribute 'cn'):

-------------------------------- snip --------------------------------
 >>> l=ldap.initialize('ldap://ldap.uninett.no')
 >>> l.protocol_version=ldap.VERSION3
 >>> l.simple_bind_s('','')
 >>> r = 
l.search_s('dc=uninett,dc=no',ldap.SCOPE_SUBTREE,'(cn=stig*)',['cn','mail','o','drink'])
 >>> r
[('uid=venaas,ou=people,dc=uninett,dc=no', {'mail': 
['stig.venas at uninett.no'], 'cn': ['Stig Ven\xc3\xa5s'], 'o': ['UNINETT']}), 
('uid=venaas,ou=users,dc=uninett,dc=no', {'cn': ['Stig Venaas']})]
 >>> dn,entry = r[0]
 >>> dn
'uid=venaas,ou=people,dc=uninett,dc=no'
 >>> entry
{'mail': ['stig.venas at uninett.no'], 'cn': ['Stig Ven\xc3\xa5s'], 'o': 
['UNINETT']}
 >>> entry['cn'][0]
'Stig Ven\xc3\xa5s'
-------------------------------- snip --------------------------------

Your mileage may vary...

Ciao, Michael.





More information about the Python-list mailing list