root/trunk/doc/examples/ldap_parallelsearch.py

Revision 324, 2.4 kB (checked in by tv, 5 years ago)

Whitespace cleanup.

  • Property svn:executable set to *
Line 
1 #!/usr/bin/python
2 # Twisted, the Framework of Your Internet
3 # Copyright (C) 2001 Matthew W. Lefkowitz
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of version 2.1 of the GNU Lesser General Public
7 # License as published by the Free Software Foundation.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
18 from ldaptor.protocols.ldap import ldapclient, ldapsyntax, ldapconnector, distinguishedname
19 from ldaptor.protocols import pureber
20 from twisted.internet import defer, reactor
21 from twisted.python import log
22 import sys
23
24 exitStatus=0
25
26 def error(fail):
27     print >>sys.stderr, 'fail:', fail.getErrorMessage()
28     global exitStatus
29     exitStatus=1
30
31 def _handle_entry(entry, connection, search):
32     sys.stdout.write("# connection %d, search %d\n%s"
33                      % (connection, search, entry))
34
35 def _search(proto, base, connection, numOfSearches):
36     l=[]
37     baseEntry = ldapsyntax.LDAPEntry(client=proto,
38                                      dn=base)
39     for search in xrange(0, numOfSearches):
40         d=baseEntry.search(callback=lambda x:
41                            _handle_entry(x, connection, search))
42         d.addErrback(error)
43         l.append(d)
44
45     dl=defer.DeferredList(l)
46     return dl
47
48 def main(base, serviceLocationOverrides, numOfConnections=3, numOfSearches=3):
49     log.startLogging(sys.stderr, setStdout=0)
50     l=[]
51     for connection in xrange(0, numOfConnections):
52         c=ldapconnector.LDAPClientCreator(reactor, ldapclient.LDAPClient)
53         d=c.connectAnonymously(base, serviceLocationOverrides)
54
55         d.addCallback(_search, base, connection, numOfSearches)
56         d.addErrback(error)
57         l.append(d)
58     dl=defer.DeferredList(l)
59     dl.addBoth(lambda dummy: reactor.stop())
60     reactor.run()
61     sys.exit(exitStatus)
62
63 if __name__ == "__main__":
64     base='dc=example,dc=com'
65     main(base=distinguishedname.DistinguishedName(base),
66          serviceLocationOverrides={base: ('localhost', None)},
67          numOfConnections=5,
68          numOfSearches=10)
Note: See TracBrowser for help on using the browser.