Tuesday, 29 January 2008

Complex LDAP queries with python

I came across this nasty problem today and the solution was so easy and stupid that it took me 10 minutes to understand what it was after many tries... :D

Say that you want to make a quite complex query to an LDAP directory using ldapsearch, the syntax is:
ldapsearch -x -h -b o=Myorganization "&(uid=john*)(!(jobgrade=boss))(address=*Washington*)"

Now to do the same thing with python ldap module I expected the syntax would be the same... well no! (don't know why?).
import ldap
l ='')
res = l.search_s('o=Myorganization', ldap.SCOPE_SUBTREE, '(&(uid=john*)(!(jobgrade=boss))(address=*Washington*))')

so you have simply to add brackets before the & (or |)... again I wonder why it was implemented differently...

No comments:

Post a Comment