sample code online

Michael Ströder michael at stroeder.com
Sun Nov 3 18:35:38 CET 2002


Michael Engelhart wrote:
> 
> I just put a very simple set of python-ldap sample code that shows 
> searching a directory, binding to a directory, and deleting entries:
> http://homepage.mac.com/mengelhart/python-ldap-samples.html

Thanks for contributing that. I've added a link to this page to 
http://python-ldap.sourceforge.net/docs.shtml .

Some comments:

As default an LDAPv2 connection is made by the OpenLDAP client libs. 
Therefore you have to explicitly set the attribute protocol_version to use 
LDAPv3. Personally I prefer to use:

   l.set_option(ldap.OPT_PROTOCOL_VERSION,ldap.VERSION3)

This is especially true in the search example:

-------------------- snip ------------------
## first you must open a connection to the server
try:
	l = ldap.open("127.0.0.1")
	## searching doesn't require a bind 	
except ldap.LDAPError, e:
	print e
	# handle error however you like
-------------------- snip ------------------

Here you state that a BindRequest is not needed before the SearchRequest. 
This is only true for LDAPv3! Most times an LDAPv2 server will give you back 
ldap.PROTOCOL_ERROR (or behave odd ;-).

It should read:

-------------------- snip ------------------
## first you must open a connection to the server
try:
	l = ldap.open("127.0.0.1")
         l.protocol_version = ldap.VERSION3
	## with LDAPv3 searching doesn't require a bind
except ldap.LDAPError, e:
	print e
	# handle error however you like
-------------------- snip ------------------

Unfortunately if LDAPv3 is not supported you will get an error at the point 
where you send the first LDAPRequest (the SearchRequest in your example).

Now one might wonder how to properly "negotiate" the LDAP protocol version. 
Well, check out how it's done ldap.ldapobject.SmartLDAPObject. This is not 
ready for prime time yet but should give you some inspiration. Please test!

Your async search example is a good starting point to lead over to deploying 
class ldap.async.List. ;-)

Well, everybody is encouraged to dig into Demo/ anyway.

Ciao, Michael.






More information about the python-ldap mailing list