ldapsearch vs. python-ldap

James james at cs.mcgill.ca
Mon Apr 6 22:48:58 CEST 2009


Hey,
I'm trying to use python-ldap to do something that works with the
ldapsearch command line tool. I've posted code samples, can someone
point out why the python-ldap fails? What is different about what
python-ldap is doing. It seems that it forgets that it just _did_ bind
successfully...
thanks in advance,

_James
james at cs.mcgill.ca


# from a command line you can successfully run:
# $ # ldapsearch -x -b "<your dc...>" -D "place\<shortname>" -h <host>
-p 389 -W
"(&(objectcategory=person)(objectclass=user)(samaccountname=<shortname>))"
# <enter password>
# <see results>

# however this doesn't work:
import ldap
import sys
import getpass

l = ldap.initialize('ldap://<host>:389')

try:
	#l.simple_bind_s('first.last at example.com', getpass.getpass())
l.simple_bind_s('<place>\<shortname>', getpass.getpass())			# Works

except Exception, e:
	print e
	print 'Bind Failed'
	sys.exit(1)

try:
	r = l.search_s('dc=<dc...>', ldap.SCOPE_SUBTREE,
'(&(objectcategory=person)(objectclass=user)(samaccountname=<shortname>))', ['*'])

except Exception, e:
	print e
	# this prints:
	# {'info': '00000000: LdapErr: DSID-0C090627, comment: In order to
perform this operation a successful bind must be completed on the
connection., data 0, vece', 'desc': 'Operations error'}
	print 'Search Died'
	sys.exit(1)

for x in r:
	print x







More information about the python-ldap mailing list