File: paged_search_ext_s.py

package info (click to toggle)
python-ldap 3.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,268 kB
  • sloc: python: 9,625; ansic: 2,828; makefile: 132; sh: 68
file content (108 lines) | stat: -rw-r--r-- 3,060 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from __future__ import print_function
url = "ldap://localhost:1390/"
base = "dc=stroeder,dc=de"
search_flt = r'(objectClass=*)'

searchreq_attrlist=['cn','entryDN','entryUUID','mail','objectClass']

from ldap.ldapobject import ReconnectLDAPObject

import ldap,pprint
from ldap.controls import SimplePagedResultsControl


class PagedResultsSearchObject:
  page_size = 50

  def paged_search_ext_s(self,base,scope,filterstr='(objectClass=*)',attrlist=None,attrsonly=0,serverctrls=None,clientctrls=None,timeout=-1,sizelimit=0):
    """
    Behaves exactly like LDAPObject.search_ext_s() but internally uses the
    simple paged results control to retrieve search results in chunks.

    This is non-sense for really large results sets which you would like
    to process one-by-one
    """

    while True: # loop for reconnecting if necessary

      req_ctrl = SimplePagedResultsControl(True,size=self.page_size,cookie='')

      try:

        # Send first search request
        msgid = self.search_ext(
          base,
          scope,
          filterstr=filterstr,
          attrlist=attrlist,
          attrsonly=attrsonly,
          serverctrls=(serverctrls or [])+[req_ctrl],
          clientctrls=clientctrls,
          timeout=timeout,
          sizelimit=sizelimit
        )

        result_pages = 0
        all_results = []

        while True:
          rtype, rdata, rmsgid, rctrls = self.result3(msgid)
          all_results.extend(rdata)
          result_pages += 1
          # Extract the simple paged results response control
          pctrls = [
            c
            for c in rctrls
            if c.controlType == SimplePagedResultsControl.controlType
          ]
          if pctrls:
            if pctrls[0].cookie:
                # Copy cookie from response control to request control
                req_ctrl.cookie = pctrls[0].cookie
                msgid = self.search_ext(
                  base,
                  scope,
                  filterstr=filterstr,
                  attrlist=attrlist,
                  attrsonly=attrsonly,
                  serverctrls=(serverctrls or [])+[req_ctrl],
                  clientctrls=clientctrls,
                  timeout=timeout,
                  sizelimit=sizelimit
                )
            else:
              break # no more pages available

      except ldap.SERVER_DOWN as e:
        try:
          self.reconnect(self._uri)
        except AttributeError:
          raise e

      else:
        return result_pages,all_results


class MyLDAPObject(ReconnectLDAPObject,PagedResultsSearchObject):
  pass


#ldap.set_option(ldap.OPT_DEBUG_LEVEL,255)
ldap.set_option(ldap.OPT_REFERRALS, 0)
l = MyLDAPObject(url,trace_level=2,retry_max=100,retry_delay=2)
l.protocol_version = 3
l.simple_bind_s("", "")
l.page_size=10

# Send search request
result_pages,all_results = l.paged_search_ext_s(
  base,
  ldap.SCOPE_SUBTREE,
  search_flt,
  attrlist=searchreq_attrlist,
  serverctrls=None
)

l.unbind_s()

print('Received %d results in %d pages.' % (len(all_results),result_pages))