本文整理汇总了Python中ldap.controls.SimplePagedResultsControl类的典型用法代码示例。如果您正苦于以下问题:Python SimplePagedResultsControl类的具体用法?Python SimplePagedResultsControl怎么用?Python SimplePagedResultsControl使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SimplePagedResultsControl类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: pagedsearch
def pagedsearch(self, search_flt, searchreq_attrlist):
req_ctrl = SimplePagedResultsControl(True,size=self.page_size,cookie='')
known_ldap_resp_ctrls = { SimplePagedResultsControl.controlType:SimplePagedResultsControl,}
# Send search request
msgid = self.lcon.search_ext( self.base, ldap.SCOPE_SUBTREE, search_flt, attrlist=searchreq_attrlist, serverctrls=[req_ctrl])
pages = 0
i = 0
while True:
pages += 1
rtype, rdata, rmsgid, serverctrls = self.lcon.result3(msgid,resp_ctrl_classes=known_ldap_resp_ctrls)
yield rdata
pctrls = [c for c in serverctrls 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.lcon.search_ext(self.base,ldap.SCOPE_SUBTREE,search_flt,attrlist=searchreq_attrlist,serverctrls=[req_ctrl])
else:
break
else:
print "Warning: Server ignores RFC 2696 control."
break
示例2: ldap_search_paged
def ldap_search_paged(l, basedn, scope, filter, attributes, timeout, page_size):
# FIXME: untested
from ldap.controls import SimplePagedResultsControl
lc = SimplePagedResultsControl(
ldap.LDAP_CONTROL_PAGE_OID, True, (page_size,'')
)
# Send search request
result_id = l.search_ext(basedn, scope, filter, attributes, serverctrls=[lc])
pages = 0
while True:
pages += 1
log.debug('Getting page %d', pages)
result_type, result_data, result_msgid, serverctrls = l.result3(result_id)
log.debug('%d results', len(result_data))
if not result_data:
break
pctrls = [c for c in serverctrls if c.controlType == ldap.LDAP_CONTROL_PAGE_OID]
if pctrls:
est, cookie = pctrls[0].controlValue
if cookie:
lc.controlValue = (page_size, cookie)
result_id = l.search_ext(basedn, scope, filter, attributes, serverctrls=[lc])
else:
break
else:
log.warn('Server ignores RFC 2696 control.')
break
示例3: _PagedAsyncSearch
def _PagedAsyncSearch(ldap_conn, sizelimit, base_dn, scope, filterstr="(objectClass=*)", attrlist=None):
""" Helper function that implements a paged LDAP search for
the Search method below.
Args:
ldap_conn: our OMLdapConnection object
sizelimit: max # of users to return.
filterstr: LDAP filter to apply to the search
attrlist: list of attributes to return. If null, all attributes
are returned
Returns:
A list of users as returned by the LDAP search
"""
# Time to autodetect our library's API, because python-ldap's API intoduced
# breaking changes between versions 2.3 and 2.4.
use_old_paging_api = False
if hasattr(ldap, "LDAP_CONTROL_PAGE_OID"):
use_old_paging_api = True
paged_results_control = SimplePagedResultsControl(
controlType=ldap.LDAP_CONTROL_PAGE_OID, criticality=True, controlValue=(_PAGE_SIZE, "")
)
page_ctrl_oid = ldap.LDAP_CONTROL_PAGE_OID
else:
paged_results_control = SimplePagedResultsControl(criticality=True, size=_PAGE_SIZE, cookie="")
page_ctrl_oid = ldap.controls.SimplePagedResultsControl.controlType
logging.debug("Paged search on %s for %s", base_dn, filterstr)
users = []
ix = 0
while True:
if _PAGE_SIZE == 0:
serverctrls = []
else:
serverctrls = [paged_results_control]
msgid = ldap_conn.conn.search_ext(base_dn, scope, filterstr, attrlist=attrlist, serverctrls=serverctrls)
res = ldap_conn.conn.result3(msgid=msgid)
unused_code, results, unused_msgid, serverctrls = res
for result in results:
ix += 1
users.append(result)
if sizelimit and ix >= sizelimit:
break
if sizelimit and ix >= sizelimit:
break
cookie = None
for serverctrl in serverctrls:
if serverctrl.controlType == page_ctrl_oid:
if use_old_paging_api:
unused_est, cookie = serverctrl.controlValue
if cookie:
paged_results_control.controlValue = (_PAGE_SIZE, cookie)
else:
cookie = paged_results_control.cookie = serverctrl.cookie
break
if not cookie:
break
return users
示例4: _paged_search
def _paged_search(self, connection, base, scope, filterstr, attrlist):
page_size = self.settings_dict.get('PAGE_SIZE', 1000)
results = []
pg_ctrl = SimplePagedResultsControl(True, page_size, "")
scope = ldap.SCOPE_SUBTREE
pages = 0
while True:
pages += 1
msgid = connection.search_ext(
base,
scope=scope,
filterstr=filterstr,
attrlist=attrlist,
serverctrls=[pg_ctrl]
)
rtype, rdata, rmsgid, serverctrls = connection.result3(msgid)
results.extend(rdata)
cookie = serverctrls[0].cookie
if cookie:
pg_ctrl.cookie = cookie
search = connection.search_ext(
base,
scope=scope,
filterstr=filterstr,
attrlist=attrlist,
serverctrls=[pg_ctrl]
)
else:
break
return results
示例5: query_users_paged
def query_users_paged(self, options):
"""
Connects to LDAP server and attempts to query and return all users
by iterating through each page result. Requires LDAP v3.
"""
page_size = options.get('page_size', 500)
criticality = options.get('criticality', True)
cookie = options.get('cookie', '')
full_record = options.get('full_record', False)
fields = self.ldap_query_fields
if full_record:
fields = None
# search the server for users
first_pass = True
pg_ctrl = SimplePagedResultsControl(criticality, page_size, cookie)
LOG.debug("self.ldap_query_fields = %r", self.ldap_query_fields)
while first_pass or pg_ctrl.cookie:
first_pass = False
msgid = self.ldap_connection.search_ext(
self.settings['base_dn'], ldap.SCOPE_SUBTREE, self.settings['filter'],
fields,
serverctrls=[pg_ctrl]
)
result_type, ldap_users, msgid, serverctrls = self.ldap_connection.result3(msgid)
pg_ctrl.cookie = serverctrls[0].cookie
for user in ldap_users:
if user[0] and user[1]:
yield _clean_record(user[1])
# disconnect and return results
self.ldap_connection.unbind_s()
示例6: paged_search_ext_s
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.
"""
req_ctrl = SimplePagedResultsControl(True, size=self.page_size,
cookie='')
# Send first search request
msgid = self.search_ext(base, ldap.SCOPE_SUBTREE, filterstr,
attrlist=attrlist,
serverctrls=(serverctrls or []) + [req_ctrl])
results = []
while True:
rtype, rdata, rmsgid, rctrls = self.result3(msgid)
results.extend(rdata)
# 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, ldap.SCOPE_SUBTREE,
filterstr, attrlist=attrlist,
serverctrls=(serverctrls or []) +
[req_ctrl])
else:
break
return results
示例7: paged_search_ext_s
def paged_search_ext_s(self, base, scope, filterstr='(objectClass=*)',
attrlist=None, attrsonly=0, serverctrls=None, clientctrls=None,
timeout=-1, sizelimit=0):
req_ctrl = SimplePagedResultsControl(
True, size=self.page_size, cookie='',
)
result_pages = 0
all_results = []
msgid = self.search_ext(
base, scope, filterstr, attrlist=attrlist,
serverctrls=(serverctrls or [])+[req_ctrl],
)
while True:
rtype, rdata, rmsgid, rctrls = self.result3(msgid)
all_results.extend(rdata)
result_pages += 1
pctrls = [c for c in rctrls if c.controlType == sprc_ct]
if not pctrls:
continue
if not pctrls[0].cookie:
break
req_ctrl.cookie = pctrls[0].cookie
msgid = self.search_ext(
base, scope, filterstr, attrlist=attrlist,
serverctrls=(serverctrls or [])+[req_ctrl]
)
return all_results
示例8: query
def query(self, base, scope, params, fixed_rdn=None):
ocs = ["(objectClass=%s)" % o.strip() for o in params['objectClasses'].split(",")]
fltr = "(&" + "".join(ocs) + (ldap.filter.filter_format("(%s)", [fixed_rdn]) if fixed_rdn else "") + ")"
result = []
with self.lh.get_handle() as l, self.lock:
self.log.debug("LDAP Search using filter '{filter}'".format(filter=fltr))
lc = SimplePagedResultsControl(criticality=True, size=self.page_size, cookie='')
ix = 0
while True:
msgid = l.search_ext(base, ldap.SCOPE_ONELEVEL, fltr, attrlist=[self.uuid_entry], serverctrls=[lc])
res = l.result3(msgid=msgid)
result_type, results, unused_msgid, serverctrls = res
for resultdata in results:
ix += 1
if resultdata[0] is not None:
result.append(resultdata)
cookie = None
for serverctrl in serverctrls:
if serverctrl.controlType == ldap.controls.libldap.SimplePagedResultsControl.controlType:
cookie = serverctrl.cookie
if cookie:
lc.cookie = cookie
break
if not cookie:
break
return [x for x in dict(result).keys()]
示例9: _paged_search_ext_s
def _paged_search_ext_s(self, base, scope, filterstr='(objectClass=*)', attrlist=None, attrsonly=0,
serverctrls=None, clientctrls=None, timeout=-1, sizelimit=0, page_size=10):
"""
Behaves similarly to LDAPObject.search_ext_s() but internally uses the
simple paged results control to retrieve search results in chunks.
Taken from the python-ldap paged_search_ext_s.py demo, showing how to use
the paged results control: https://bitbucket.org/jaraco/python-ldap/
"""
request_ctrl = SimplePagedResultsControl(True, size=page_size, cookie='')
results = []
while True:
msgid = self.conn.search_ext(base, scope, filterstr=filterstr, attrlist=attrlist, attrsonly=attrsonly,
serverctrls=(serverctrls or []) + [request_ctrl], clientctrls=clientctrls,
timeout=timeout, sizelimit=sizelimit)
result_type, result_data, result_msgid, result_ctrls = self.conn.result3(msgid)
results.extend(result_data)
# Extract the simple paged results response control
paged_ctrls = [c for c in result_ctrls if c.controlType == SimplePagedResultsControl.controlType]
if paged_ctrls and paged_ctrls[0].cookie:
# Copy cookie from response control to request control
request_ctrl.cookie = paged_ctrls[0].cookie
else:
break
return results
示例10: search
def search(base_dn, search_filter, attributes):
"""Iterative LDAP search using page control.
:param base_dn: str -- The base DN from which to start the search.
:param search_filter: str -- Representation of the filter to apply
in the search.
:param attributes: list -- Attributes to be retrieved for each
entry. If ``None``, all attributes will be
retrieved.
:returns: A generator which yields one search result at a time as a
tuple containing a `dn` as ``str`` and `attributes` as
``dict``.
"""
connection, settings = current_ldap
page_ctrl = SimplePagedResultsControl(True, size=settings['page_size'], cookie='')
while True:
msg_id = connection.search_ext(base_dn, SCOPE_SUBTREE, filterstr=search_filter, attrlist=attributes,
serverctrls=[page_ctrl], timeout=settings['timeout'])
try:
_, r_data, __, server_ctrls = connection.result3(msg_id, timeout=settings['timeout'])
except NO_SUCH_OBJECT:
break
for dn, entry in r_data:
if dn:
yield dn, entry
page_ctrl.cookie = get_page_cookie(server_ctrls)
if not page_ctrl.cookie:
# End of results
break
示例11: paged_search_ext_s
def paged_search_ext_s(self, conn, base, scope, filter, attrs, attrsonly=0,serverctrls=None,clientctrls=None,timeout=-1,sizelimit=0):
""" Helper function that implements a paged LDAP search for
the Search method below.
Args:
query: LDAP filter to apply to the search
sizelimit: max # of users to return.
attrlist: list of attributes to return. If null, all attributes
are returned
Returns:
A list of users as returned by the LDAP search
"""
ldap_page_size = 100
search_flt = r'(objectClass=*)'
searchreq_attrlist = ['cn', 'entryDN',
'entryUUID', 'mail', 'objectClass']
req_ctrl = SimplePagedResultsControl(
True, ldap_page_size, cookie='')
logging.debug('Paged search on %s for %s' % (base, filter))
# Send first search request
msgid = conn.search_ext(
base,
ldap.SCOPE_SUBTREE,
search_flt,
attrlist=searchreq_attrlist,
serverctrls=(serverctrls or []) + [req_ctrl]
)
result_pages = 0
all_results = []
while True:
rtype, rdata, rmsgid, rctrls = conn.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 = conn.search_ext(
base,
ldap.SCOPE_SUBTREE,
search_flt,
attrlist=searchreq_attrlist,
serverctrls=(serverctrls or []) + [req_ctrl]
)
else:
break
return all_results
示例12: paged_search
def paged_search(self, base, sfilter, attrlist, scope='subtree', page_size=1000):
if scope == 'one':
_scope = ldap.SCOPE_ONELEVEL
else:
_scope = ldap.SCOPE_SUBTREE
lc = SimplePagedResultsControl(
ldap.LDAP_CONTROL_PAGE_OID,True,(page_size,'')
)
# Send search request
msgid = self.l.search_ext(
base,
_scope,
sfilter,
attrlist=attrlist,
serverctrls=[lc]
)
results = []
pages = 0
while True:
pages += 1
#print "Getting page %d" % (pages,)
rtype, rdata, rmsgid, serverctrls = self.l.result3(msgid)
#print '%d results' % len(rdata)
for dn,data in rdata:
_r = data
_r['dn'] = dn
results.append(_r)
#results += [i[0] for i in rdata]
#pprint.pprint(rdata[0])
pctrls = [
c
for c in serverctrls
if c.controlType == ldap.LDAP_CONTROL_PAGE_OID
]
if pctrls:
est, cookie = pctrls[0].controlValue
if cookie:
lc.controlValue = (page_size, cookie)
msgid = l.search_ext(
base,
_scope,
sfilter,
attrlist=attrlist,
serverctrls=[lc]
)
else:
break
else:
print "Warning: Server ignores RFC 2696 control."
break
return results
示例13: _paged_search
def _paged_search(self, base_dn, scope, search_filter, attrs):
conn = self.connect()
# Get paged results to prevent exceeding server size limit
page_size = 1000
if PYTHON_LDAP_24:
lc = SimplePagedResultsControl(size=page_size, cookie='')
else:
lc = SimplePagedResultsControl(LDAP_CONTROL_PAGED_RESULTS,
True,
(page_size, ''),)
is_last_page = False
results = []
while not is_last_page:
msgid = conn.search_ext(base_dn,
scope,
search_filter,
attrs,
serverctrls=[lc])
rtype, rdata, rmsgid, serverctrls = conn.result3(msgid)
pctrls = [c for c in serverctrls
if c.controlType == LDAP_CONTROL_PAGED_RESULTS]
results.extend(rdata)
if pctrls:
if PYTHON_LDAP_24:
cookie = pctrls[0].cookie
if cookie:
lc.cookie = cookie
else:
is_last_page = True
else:
cookie = pctrls[0].controlValue[1]
if cookie:
# lc.controlValue seems to have been mutable at some
# point, now it's a tuple.
cv = list(lc.controlValue)
cv[1] = cookie
lc.controlValue = tuple(cv)
else:
is_last_page = True
else:
is_last_page = True
if results:
# If the search returned an empty result, page controls
# aren't included - so don't produce a bogus warning
logger.warn(
"Server ignores paged results control (RFC 2696).")
return results
示例14: _search
def _search(self, basedn='', scope=ldap.SCOPE_SUBTREE, filter='', timeout=-1, sizelimit=0):
if not self._handle:
self._open()
result = []
serverctrls = None
clientctrls = None
paged = SimplePagedResultsControl(
criticality=False,
size=self.pagesize,
cookie=''
)
paged_ctrls = {SimplePagedResultsControl.controlType: SimplePagedResultsControl}
page = 0
while True:
serverctrls = [paged]
id = self._handle.search_ext(
basedn,
scope,
filterstr=filter,
attrlist=None,
attrsonly=0,
serverctrls=serverctrls,
clientctrls=clientctrls,
timeout=timeout,
sizelimit=sizelimit
)
(rtype, rdata, rmsgid, serverctrls) = self._handle.result3(
id, resp_ctrl_classes=paged_ctrls
)
result.extend(rdata)
paged.size = 0
paged.cookie = cookie = None
for sc in serverctrls:
if sc.controlType == SimplePagedResultsControl.controlType:
cookie = sc.cookie
if cookie:
paged.cookie = cookie
paged.size = self.pagesize
break
if not cookie:
break
page += 1
return result
示例15: paged_search
def paged_search(self, search_dn, filter, scope, results_processor, attributes=None):
if not attributes:
attributes = ['*']
page_control = SimplePagedResultsControl(True, self.PAGE_SIZE, '')
serverctrls = [page_control]
msgid = self.connection.search_ext(
search_dn,
scope,
filter,
attrlist=attributes,
serverctrls=serverctrls
)
page = 0
records = 0
while True:
page += 1
try:
result_type, results, result_msg, serverctrls = self.connection.result3(msgid=msgid, timeout=self.LDAP_TIMEOUT)
records += len(results)
results_processor(results)
pagectrls = [
c
for c in serverctrls
if c.controlType == SimplePagedResultsControl.controlType
]
if pagectrls:
if pagectrls[0].cookie:
page_control.cookie = pagectrls[0].cookie
msgid = self.connection.search_ext(
search_dn,
scope,
filter,
attrlist=attributes,
serverctrls=[page_control]
)
else:
break
else:
break
except ldap.LDAPError, e:
print e