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

Notice! PyPM is being replaced with the ActiveState Platform, which enhances PyPM’s build and deploy capabilities. Create your free Platform account to download ActivePython or customize Python with the packages you require and get automatic updates.

Download
ActivePython
INSTALL>
pypm install bda.ldap

How to install bda.ldap

  1. Download and install ActivePython
  2. Open Command Prompt
  3. Type pypm install bda.ldap
 Python 2.7Python 3.2Python 3.3
Windows (32-bit)
Windows (64-bit)
Mac OS X (10.5+)
1.5.2 Available View build log
Linux (32-bit)
Linux (64-bit)
Web
 
License
General Public Licence
Lastest release
version 1.5.2 on Feb 20th, 2011

LDAP convenience library

This Package provides objects for LDAP communication.

LDAP Session

You can work with the LDAPSession object.

>>> from bda.ldap import ONELEVEL
>>> from bda.ldap import LDAPSession
>>> from bda.ldap import LDAPProps
>>> props = LDAPProps('localhost',
...                   389,
...                   'cn=user,dc=example,dc=com',
...                   'secret'
...                   cache=True,
...                   timeout=12345)
>>> session = LDAPSession(props)
>>> res = session.search('(uid=*)', ONELEVEL)
LDAP Node

You can build and edit LDAP data trees with the LDAPNode which is based on zodict.Node.

The root Node expects the base DN and the server properties to initialize.

>>> from bda.ldap import LDAPNode
>>> root = LDAPNode('dc=my-domain,dc=com', props=props)
>>> root.keys()

System Message: ERROR/3 (<string>, line 37)

Inconsistent literal block quoting.

['ou=customers']

You can create and add new LDAP entries.

>>> person = LDAPNode()
>>> person.attributes['objectClass'] = ['top', 'person']
>>> person.attributes['sn'] = 'Mustermann'
>>> person.attributes['cn'] = 'Max'
>>> person.attributes['description'] = 'Description'
>>> customers['cn=max'] = person
>>> customers.keys()

System Message: ERROR/3 (<string>, line 49)

Inconsistent literal block quoting.

['cn=max']

On __call__ the modifications of the tree are written to the directory.

>>> customers()

Modification of entry attributes.

>>> person.attributes['description'] = 'Another description'
>>> person()
>>> del person.attributes['description']
>>> person()

Deleting of entries.

>>> del customers['cn=max']
>>> customers()

For more details see the corresponding source and test files.

Character Encoding

LDAP (v3 at least, RFC 2251) uses utf8 string encoding. Since 1.5.1, LDAPSession and LDAPNode translate these to unicodes for you. Consider it a bug, if you receive anything else than unicode from LDAPSession or LDAPNode. Everything below that LDAPConnector and LDAPCommunicator give you the real ldap experience. - Should we change that, too?

Unicode strings you pass to nodes or sessions are automatically encoded to uft8 for LDAP. If you feed them normal strings they are decoded as utf8 and reencoded as utf8 to make sure they are utf8 or compatible, e.g. ascii.

If decoding as utf8 fails, the value is assumed to be in binary and left as a string (see TODO).

If you have an LDAP server that does not use utf8, monkey-patch bda.ldap.strcodec.LDAP_CHARACTER_ENCODING.

If you are confused by all that encoding/decoding: python knows in what encoding it stores its unicodes, however, it cannot know for normal strs. Therefore, you should only use unicodes. In order to get a unicode for a str, a string is decoded according to a given encoding schema (eg utf8). And encoding a unicode produces a str in a specific encoding (eg utf8).

Caching Support

bda.ldap caches LDAP searches using the lightweight bda.cache. You need to provide a utility in you application in order to make caching work. If you dont, bda.ldap falls back to use the NullCache, which does not cache anything.

To provide an cache based on Memcached install the memcached server, configure and start it. I suppose its started on localhost port 11211 (which is a common default). Then you need to provide a utility acting as a factory.

>>> from bda.ldap.cache import MemcachedProviderFactory
>>> providerfactory = MemcachedProviderFactory()
>>> from zope.component import provideUtility
>>> provideUtility(providerfactory)

In the case you have more than one memcached server running or hav it running on another maschine, you need to initialize the factory different:

>>> providerfactory = MemcachedProviderFactory(servers=[10.0.0.10:22122,

System Message: ERROR/3 (<string>, line 124)

Inconsistent literal block quoting.

... 10.0.0.11:22322]) >>> provideUtility(providerfactory)

Dependencies

  • python-ldap
  • zodict
  • bda.cache

Notes on python-ldap

Although python-ldap is available via pypi, we excluded it from install_requires due to different compile issues on different platforms.

So you have to make sure that pyhon-ldap is available on your system in any way.

TODO

  • TLS/SSL Support. in LDAPConnector

System Message: WARNING/2 (<string>, line 152)

Bullet list ends without a blank line; unexpected unindent.

could be useful: python-ldap's class SmartLDAPObject(ReconnectLDAPObject) - Mainly the __init__() method does some smarter things like negotiating the LDAP protocol version and calling LDAPObject.start_tls_s().

  • Improve retry logic in LDAPSession

System Message: WARNING/2 (<string>, line 157)

Bullet list ends without a blank line; unexpected unindent.

could be useful, python-ldap's class ReconnectLDAPObject(SimpleLDAPObject) - In case of server failure (ldap.SERVER_DOWN) the implementations of all synchronous operation methods (search_s() etc.) are doing an automatic reconnect and rebind and will retry the very same operation.

  • Extend LDAPSession object to handle Fallback server(s)
  • Encoding/Decoding the data sent to ldap changed the order of dict entries,

System Message: WARNING/2 (<string>, line 165)

Bullet list ends without a blank line; unexpected unindent.

probably due to dict implementation. Investigate effects of that. I had the impression so far that ldap (at least openldap) preserves the order if you give it an ldif file. Iff, then python-ldap should use odicts not dicts.

  • check/implement silent sort on only the keys LDAPNode.sortonkeys()

  • binary attributes: 1. introduce Binary: ``node['cn=foo'].attributes['image']

    System Message: WARNING/2 (<string>, line 171); backlink

    Inline literal start-string without end-string.

System Message: WARNING/2 (<string>, line 172)

Bullet list ends without a blank line; unexpected unindent.

= Binary(stream)``, 2. parse ldap schema to identify binary attributes

Changes

1.5.2
  • assume strings that fail to decode to be binary and leave them as-is

System Message: WARNING/2 (<string>, line 182)

Bullet list ends without a blank line; unexpected unindent.

(chaoflow, 2010-07-19)

  • session.search, default filter '(objectClass=*)' and scope BASE, i.e.

System Message: WARNING/2 (<string>, line 185)

Bullet list ends without a blank line; unexpected unindent.

just calling search returns the basedn entry. Further it is possible to call session.search(scope=ONELEVEL) to get all entries one level below the basedn. (chaoflow, 2010-07-19)

1.5.1
  • character encoding: LDAPSession and LDAPNode only return unicode and

System Message: WARNING/2 (<string>, line 193)

Bullet list ends without a blank line; unexpected unindent.

enforces utf8 or compatible encoding on all strings they receive, see Character Encoding. (chaoflow, 2010-07-17)

  • introduced strcodec module for unicode->str->unicode translation

System Message: WARNING/2 (<string>, line 198)

Bullet list ends without a blank line; unexpected unindent.

(chaoflow, 2010-07-17)

  • add LDAPNode.get to use LDAPNode.__getitem__ instead of odict's

System Message: WARNING/2 (<string>, line 201)

Bullet list ends without a blank line; unexpected unindent.

(chaoflow, 2010-07-16)

  • more tests, explode_dn for dn handling (with spaces and escaped commas)

System Message: WARNING/2 (<string>, line 204)

Bullet list ends without a blank line; unexpected unindent.

(chaoflow, 2010-07-16)

  • ignore results with dn=None. ActiveDirectory produces them

System Message: WARNING/2 (<string>, line 207)

Bullet list ends without a blank line; unexpected unindent.

(chaoflow, 2010-07-15)

  • default filter for session.search, if you pass '', u'' or None as filter

System Message: WARNING/2 (<string>, line 210)

Bullet list ends without a blank line; unexpected unindent.

(chaoflow, 2010-07-15)

  • tests for attrlist and attrsonly

System Message: WARNING/2 (<string>, line 213)

Bullet list ends without a blank line; unexpected unindent.

(chaoflow, 2010-07-15)

  • adopt for latest zodict.

System Message: WARNING/2 (<string>, line 216)

Bullet list ends without a blank line; unexpected unindent.

(rnix, 2010-07-15)

  • added support for sort to node. Note: This wakes up all children of Node.

System Message: WARNING/2 (<string>, line 219)

Bullet list ends without a blank line; unexpected unindent.

(jensens, 2010-04-16)

  • added support for "items() to Node".

System Message: WARNING/2 (<string>, line 222)

Bullet list ends without a blank line; unexpected unindent.

(jensens, 2010-04-16)

  • BBB compatibility for zope2.9

System Message: WARNING/2 (<string>, line 225)

Bullet list ends without a blank line; unexpected unindent.

(rnix, jensens, 2010-02-17)

  • If a Node was added and no child added __iter__ failed. Fixed now.

System Message: WARNING/2 (<string>, line 228)

Bullet list ends without a blank line; unexpected unindent.

(jensens, 2010-01-19)

  • If a Node was added we cant load its attributes. Takes this into account now.

System Message: WARNING/2 (<string>, line 231)

Bullet list ends without a blank line; unexpected unindent.

(jensens, 2010-01-17)

1.5.0
  • Made MemcachedProviderFactory configureable. Defaults behave like in prior

System Message: WARNING/2 (<string>, line 237)

Bullet list ends without a blank line; unexpected unindent.

versions. New: We can pass server= keyword argument to the constructor expecting a list of servers, each in the form server:port. (jensens, 2009-12-30)

  • Dont provide any cache provider factory by default. Added a

System Message: WARNING/2 (<string>, line 242)

Bullet list ends without a blank line; unexpected unindent.

nullCacheProviderFactory which provides a non-caching behaviour. Use this as fallback if no utility was registered. (jensens, 2009-12-30)

  • Add read property ldap_session to LDAPNode. This way its clean to take

System Message: WARNING/2 (<string>, line 247)

Bullet list ends without a blank line; unexpected unindent.

the session of LDAPNode in an application i.e. for searching. Be careful while using the session directly to manipulate the LDAP; responsibility to invalidate the LDAPNode instances is on the application developer. (jensens, 2009-12-30)

1.4.0
  • Add LDAPProps object. Its points to LDAPServerProperties class. The

System Message: WARNING/2 (<string>, line 256)

Bullet list ends without a blank line; unexpected unindent.

latter one will be renamed to LDAPProps in version 1.5. Too long class name. (rnix, 2009-12-23)

  • Add LDAPSession.unbind function. (rnix, 2009-12-23)
  • Add some tests for LDAPSession. (rnix, 2009-12-23)
  • Remove deprecated cache kwarg from LDAPSession.__init__.. Cache

System Message: WARNING/2 (<string>, line 264)

Bullet list ends without a blank line; unexpected unindent.

timeout and flag if cache is enabled is done due to LDAPServerProperties. (rnix, 2009-12-23)

  • Deprecate Signature of LDAPConnector.__init__. (rnix, 2009-12-23)
  • Deprecate LDAPConnector.setProtocol, LDAPCommunicator.setBaseDN,

System Message: WARNING/2 (<string>, line 270)

Bullet list ends without a blank line; unexpected unindent.

LDAPCommunicator.getBaseDN, LDAPSession.setBaseDN. (rnix, 2009-12-23)

  • Refactor the whole LDAPNode to use zodict.LifecycleNode. Clean up of

System Message: WARNING/2 (<string>, line 273)

Bullet list ends without a blank line; unexpected unindent.

the LDAPNode code. (jensens, rnix, 2009-12-22)

  • improved stop mechanism of ldap server in tests (jensens, 2009-12-16)
  • remove deprecation warning: use hashlib for md5 and fallback to md5

System Message: WARNING/2 (<string>, line 278)

Bullet list ends without a blank line; unexpected unindent.

with python2.4. (jensens, 2009-12-16)

1.3.2
  • handle timeout of cache, workaround atm (rnix, 2009-09-02)
1.3.1
  • add cache property to LDAPProperties. (rnix, 2009-05-08)
  • modify session to fit this new cache property. (rnix, 2009-05-07)
  • add queryNode function. (rnix, 2009-05-07)
  • add get function to node, this failed due LDAP read logic.

System Message: WARNING/2 (<string>, line 295)

Bullet list ends without a blank line; unexpected unindent.

(rnix, 2009-05-07)

1.3
  • support attrlist and attrsonly for search functions.

System Message: WARNING/2 (<string>, line 301)

Bullet list ends without a blank line; unexpected unindent.

(rnix, 2009-04-16)

  • add LDAPEntry object. (rnix, 2009-04-16)
  • add search base to cache key. (rnix, 2009-04-16)
1.2.3
  • bugfix in LDAPSession. Pass force_reload to relevant execution

System Message: WARNING/2 (<string>, line 311)

Bullet list ends without a blank line; unexpected unindent.

function. (rnix, 2009-02-11)

1.2.2
  • add buildout for standalone testing. (rnix, jensens - 2009-02-11)
  • add tests. (rnix, jensens - 2009-02-11)
  • provide relevant objects via package __init__.

System Message: WARNING/2 (<string>, line 321)

Bullet list ends without a blank line; unexpected unindent.

(rnix, jensens - 2009-02-11)

1.2.1
  • provide same search() signature in LDAPSession as

System Message: WARNING/2 (<string>, line 327)

Bullet list ends without a blank line; unexpected unindent.

in LDAPCommunicator. (rnix - 2009-02-10)

  • log only on debug. (rnix - 2009-02-10)
<= 1.2
  • make it work.

System Message: WARNING/2 (<string>, line 335)

Bullet list ends without a blank line; unexpected unindent.

(all contributors)

Credits

Subscribe to package updates

Last updated Feb 20th, 2011

Download Stats

Last month:1

What does the lock icon mean?

Builds marked with a lock icon are only available via PyPM to users with a current ActivePython Business Edition subscription.

Need custom builds or support?

ActivePython Enterprise Edition guarantees priority access to technical support, indemnification, expert consulting and quality-assured language builds.

Plan on re-distributing ActivePython?

Get re-distribution rights and eliminate legal risks with ActivePython OEM Edition.