LDAP Polling

Yeargan Yancey yancey at unt.edu
Tue Jan 29 02:01:13 CET 2008

I am trying to use the asynchronous LDAP polling feature and have a  
question about what appears to be a timing issue. When I run the code  
below, I find that I must insert a sleep() before entering the while  
loop or I get a ValueError exception.

If I sleep for around 0.01 seconds, the exception is intermittent  
(from my system to this server). With the sleep at 0.10 seconds or  
higher, I get the results I would expect from the server.

I would expect that whether I sleep or not should be irrelevant. If I  
poll for a result and there is nothing available, then result()  
should return with None and not generate an exception. Is that the  
correct behavior? What am I missing?

I provided an example of the code output including the exception  
itself and the sample code below. The code uses a public LDAP server  
and should work from anywhere.

---=[begin: sample exception]=---
Polling ...
Traceback (most recent call last):
   File "./sample.py", line 26, in ?
     res = l.result(res_id, all=0, timeout=0)
   File "/usr/lib/python2.4/site-packages/ldap/ldapobject.py", line  
405, in result
     res_type,res_data,res_msgid = self.result2(msgid,all,timeout)
   File "/usr/lib/python2.4/site-packages/ldap/ldapobject.py", line  
409, in result2
     res_type, res_data, res_msgid, srv_ctrls = self.result3 
   File "/usr/lib/python2.4/site-packages/ldap/ldapobject.py", line  
415, in result3
     rtype, rdata, rmsgid, serverctrls = self._ldap_call 
ValueError: need more than 3 values to unpack
---=[end: sample exception]=---

---=[begin: sample code]=---

import ldap,time

l = ldap.initialize('ldap://ldap.utexas.edu')

l.simple_bind_s() #anonymous bind

base = r'ou=people,dc=directory,dc=utexas,dc=edu'
scope = ldap.SCOPE_ONELEVEL
filt = r'(&(uid=yw*)(objectclass=inetOrgPerson))'
attrs = ['givenname']

res_id = l.search(base, scope, filt, attrs)

# exception occurs without this sleep -- WHY??

while 1:

     print 'Polling ...'

     # Set all=0 to retrieve a single entry
     # Set timeout=0 to poll for a response
     res = l.result(res_id, all=0, timeout=0)

     if type(res) == tuple:
         if res[0] == ldap.RES_SEARCH_ENTRY:
             print 'Got LDAP ENTRY'
             print 'Data: ', res[1]
         elif res[0] == ldap.RES_SEARCH_RESULT:
             print 'Got LDAP RESULT'
             print 'Data: ',
             if res[1] == []:
                 print '--empty--'
                 print res[1]
             break  #exit while loop
             print '-'*60
             print '*** UNEXPECTED LDAP RESULT ***'
             print 'DEBUG (res): ', res
             print '-'*60
         print 'DEBUG: type = ', type(res)
         print 'DEBUG: value = ', repr(res)

---=[end: sample code]=---

Any help appreciated.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ldap/attachments/20080128/30c98ca4/attachment.html>

More information about the python-ldap mailing list