[Twisted-Python] newbie -- trying to do async ldap operations

Tommi Virtanen tv-nospam.da39a3ee5e6b4b0d3255bfef95601890afd80709 at tv.debian.net
Thu Apr 24 01:42:01 MDT 2003


On Wed, Apr 23, 2003 at 05:00:52PM -0500, Allan Streib wrote:
> class LDAPQueryTracker:
>     def __init__(self):
>         self._ids = {}
>         self.ldapCon = ldap.initialize('ldap://localhost/')
>         self.ldapCon.set_option(ldap.OPT_PROTOCOL_VERSION, 3)
>         self.ldapCon.simple_bind_s('cn=Manager,dc=iu,dc=edu', 'xxxxxx')

	This blocks right here; maybe it blocks in the ldap.initialize part, too.

	And it's error return mechanism seems to be by way of
	exceptions, so making it async will change the API, too.

> #A simple callback
> def printResults(results):
>     for result in results:
>         print result
> 
> def printFailure(failure):
>     print "LDAP Operation failed"
>     failure.printTraceback()
> 
> 
> qt = LDAPQueryTracker()
> deferedResult = qt.search({'base': 'ou=people,dc=iu,dc=edu',
>                            'filter': '(uid=astreib)'})
> deferedResult.addCallbacks(printResults, printFailure)
> 
> reactor.run()

	Doing the same with Ldaptor (not tested):

class Search(ldapclient.LDAPClient):
    def connectionMade(self):
	d=self.bind()
	d.addCallback(self._handle_bind_success)
	d.addErrback(self.factory.deferred.errback)

    def _handle_bind_success(self, x):
	matchedDN, serverSaslCreds = x
	o=ldapsyntax.LDAPObject(client=self,
				dn='ou=people,dc=iu,dc=edu')
	d=o.search(filterText='(uid=astreib)')
	d.chainDeferred(self.factory.deferred)

class SearchFactory(protocol.ClientFactory):
    protocol = Search

    def __init__(self, deferred):
	self.deferred=deferred

    def clientConnectionFailed(self, connector, reason):
	self.deferred.errback(reason)

d=defer.Deferred()
s=SearchFactory(d)
d.addCallbacks(printResults, printFailure)
d.addBoth(lambda x: reactor.stop())

# service discovery from DNS, no need to know host name of server
dn = distinguishedname.DistinguishedName(stringValue='ou=people,dc=iu,dc=edu')
c = ldapconnector.LDAPConnector(reactor, dn, s)
c.connect()

reactor.run()


-- 
:(){ :|:&};:




More information about the Twisted-Python mailing list