File: ldap_parallelsearch.py

package info (click to toggle)
ldaptor 0.0.43%2Bdebian1-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,168 kB
  • ctags: 3,592
  • sloc: python: 18,670; xml: 787; makefile: 147; sh: 88
file content (68 lines) | stat: -rwxr-xr-x 2,456 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/python
# Twisted, the Framework of Your Internet
# Copyright (C) 2001 Matthew W. Lefkowitz
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of version 2.1 of the GNU Lesser General Public
# License as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

from ldaptor.protocols.ldap import ldapclient, ldapsyntax, ldapconnector, distinguishedname
from ldaptor.protocols import pureber
from twisted.internet import defer, reactor
from twisted.python import log
import sys

exitStatus=0

def error(fail):
    print >>sys.stderr, 'fail:', fail.getErrorMessage()
    global exitStatus
    exitStatus=1

def _handle_entry(entry, connection, search):
    sys.stdout.write("# connection %d, search %d\n%s"
                     % (connection, search, entry))

def _search(proto, base, connection, numOfSearches):
    l=[]
    baseEntry = ldapsyntax.LDAPEntry(client=proto,
                                     dn=base)
    for search in xrange(0, numOfSearches):
        d=baseEntry.search(callback=lambda x:
                           _handle_entry(x, connection, search))
        d.addErrback(error)
        l.append(d)

    dl=defer.DeferredList(l)
    return dl

def main(base, serviceLocationOverrides, numOfConnections=3, numOfSearches=3):
    log.startLogging(sys.stderr, setStdout=0)
    l=[]
    for connection in xrange(0, numOfConnections):
        c=ldapconnector.LDAPClientCreator(reactor, ldapclient.LDAPClient)
        d=c.connectAnonymously(base, serviceLocationOverrides)

        d.addCallback(_search, base, connection, numOfSearches)
        d.addErrback(error)
        l.append(d)
    dl=defer.DeferredList(l)
    dl.addBoth(lambda dummy: reactor.stop())
    reactor.run()
    sys.exit(exitStatus)

if __name__ == "__main__":
    base='dc=example,dc=com'
    main(base=distinguishedname.DistinguishedName(base),
         serviceLocationOverrides={base: ('localhost', None)},
         numOfConnections=5,
         numOfSearches=10)