Python ldap pointers for a newbie - Actually just trying to decifer the error..

R

rh0dium

Hi all,

I have a script which appears to work but it errors and the following
output is given. My code is listed below..

Traceback (most recent call last):
File "./ldap-nsc2.py", line 96, in ?
truc.search()
File "./ldap-nsc2.py", line 49, in search
(result_type, result_data) = cnx.result(ldap_result_id, 0)
File "/usr/local/lib/python2.4/site-packages/ldap/ldapobject.py",
line 406, in result
res_type,res_data,res_msgid = self.result2(msgid,all,timeout)
File "/usr/local/lib/python2.4/site-packages/ldap/ldapobject.py",
line 410, in result2
res_type, res_data, res_msgid, srv_ctrls =
self.result3(msgid,all,timeout)
File "/usr/local/lib/python2.4/site-packages/ldap/ldapobject.py",
line 416, in result3
rtype, rdata, rmsgid, serverctrls =
self._ldap_call(self._l.result3,msgid,all,timeout)
File "/usr/local/lib/python2.4/site-packages/ldap/ldapobject.py",
line 94, in _ldap_call
result = func(*args,**kwargs)
TypeError: an integer is required

So I dig into the docs at
/usr/local/lib/python2.4/site-packages/ldap/ldapobject.py to see what
the problem is..

<snip>
def result(self,msgid=ldap.RES_ANY,all=1,timeout=None):
"""
result([msgid=RES_ANY [,all=1 [,timeout=None]]]) -> (result_type,
result_data)
.
.
.
"""
<snip>

So.. That's what I have in my code..

result_type, result_data = cnx.result(ldap_result_id,
0)


Can Someone please point me in the right direction. I want to know WHY
it's failing. I think it's right? Anyone?


My Code...


----------------------------
import ldap

class NSCLdap(object):
def __init__(self, server="sc-ldap.nsc.com", baseDN="o=nsc.com",
who=None, cred=None):
self.server = server
self.baseDN = baseDN
if who is None:
self.who = ""
else:
self.who = who
if cred is None:
self.cred = ""
else:
self.cred = cred
self.connection = None

def connect(self):
print "LDAP Version", ldap.__version__
self.connection = ldap.open(self.server)
self.connection.simple_bind_s( self.who, self.cred)
self.connection.protocol_version=ldap.VERSION3

def search(self, baseDN=None, searchScope=ldap.SCOPE_SUBTREE,
retrieveAttrs=None, searchAttrs="l=ny5*" ):
cnx = self.connection
if baseDN is None:
baseDN = self.baseDN
try:
ldap_result_id = cnx.search_s(baseDN, searchScope,
searchAttrs, retrieveAttrs)
while 1:
result_type, result_data = cnx.result(ldap_result_id,
0)
if (result_data == []):
break
else:
if result_type == ldap.RES_SEARCH_ENTRY:
result_set.append(result_data)

if len(result_set) == 0:
print "No Results."
return

for i in range(len(result_set)):
for entry in result_set:
try:
name = entry[0]['cn']
email = entry[0]['mail']
phone = entry[0]['telephonenumber']
loc = entry[0]['l']
count = count + 1

print "%d.\nName: %s\nDescription: %s\nE-mail:
%s\nPhone: %s\n" %\
(count, name, desc, email, phone)
except:
pass
except ldap.LDAPError, error_message:
print error_message

if __name__ == '__main__':
truc = NSCLdap()
truc.connect()
truc.search()
 
?

=?ISO-8859-1?Q?Michael_Str=F6der?=

rh0dium said:
ldap_result_id = cnx.search_s(baseDN, searchScope, searchAttrs, retrieveAttrs)

You are already using the synchronous search method which indeed return
the search results.

So this should read:

result_data=cnx.search_s(baseDN,searchScope,searchAttrs,retrieveAttrs)
result_type, result_data = cnx.result(ldap_result_id, 0)

You don't need this if you just want to use synchronous search method.

Ciao, Michael.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top