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...

1 comment:

  1. Hello I am so delighted I located your blog, I really located you by mistake, while I was watching on google for something else, Anyways I am here now and could just like to say thank for a tremendous post and a all round entertaining website. Please do keep up the great work. 金融作业代写