本文整理汇总了Python中ldap.controls.SimplePagedResultsControl.cookie方法的典型用法代码示例。如果您正苦于以下问题:Python SimplePagedResultsControl.cookie方法的具体用法?Python SimplePagedResultsControl.cookie怎么用?Python SimplePagedResultsControl.cookie使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ldap.controls.SimplePagedResultsControl
的用法示例。
在下文中一共展示了SimplePagedResultsControl.cookie方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: query_users_paged
# 需要导入模块: from ldap.controls import SimplePagedResultsControl [as 别名]
# 或者: from ldap.controls.SimplePagedResultsControl import cookie [as 别名]
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()
示例2: query
# 需要导入模块: from ldap.controls import SimplePagedResultsControl [as 别名]
# 或者: from ldap.controls.SimplePagedResultsControl import cookie [as 别名]
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()]
示例3: search
# 需要导入模块: from ldap.controls import SimplePagedResultsControl [as 别名]
# 或者: from ldap.controls.SimplePagedResultsControl import cookie [as 别名]
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
示例4: paged_search_ext_s
# 需要导入模块: from ldap.controls import SimplePagedResultsControl [as 别名]
# 或者: from ldap.controls.SimplePagedResultsControl import cookie [as 别名]
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
示例5: pagedsearch
# 需要导入模块: from ldap.controls import SimplePagedResultsControl [as 别名]
# 或者: from ldap.controls.SimplePagedResultsControl import cookie [as 别名]
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
示例6: paged_search_ext_s
# 需要导入模块: from ldap.controls import SimplePagedResultsControl [as 别名]
# 或者: from ldap.controls.SimplePagedResultsControl import cookie [as 别名]
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
示例7: _paged_search
# 需要导入模块: from ldap.controls import SimplePagedResultsControl [as 别名]
# 或者: from ldap.controls.SimplePagedResultsControl import cookie [as 别名]
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
示例8: _paged_search_ext_s
# 需要导入模块: from ldap.controls import SimplePagedResultsControl [as 别名]
# 或者: from ldap.controls.SimplePagedResultsControl import cookie [as 别名]
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
示例9: paged_search_ext_s
# 需要导入模块: from ldap.controls import SimplePagedResultsControl [as 别名]
# 或者: from ldap.controls.SimplePagedResultsControl import cookie [as 别名]
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
示例10: _search
# 需要导入模块: from ldap.controls import SimplePagedResultsControl [as 别名]
# 或者: from ldap.controls.SimplePagedResultsControl import cookie [as 别名]
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
示例11: _paged_search
# 需要导入模块: from ldap.controls import SimplePagedResultsControl [as 别名]
# 或者: from ldap.controls.SimplePagedResultsControl import cookie [as 别名]
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
示例12: paged_search
# 需要导入模块: from ldap.controls import SimplePagedResultsControl [as 别名]
# 或者: from ldap.controls.SimplePagedResultsControl import cookie [as 别名]
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
示例13: paged_search_ext_s
# 需要导入模块: from ldap.controls import SimplePagedResultsControl [as 别名]
# 或者: from ldap.controls.SimplePagedResultsControl import cookie [as 别名]
def paged_search_ext_s(self,base,scope,filterstr='(objectClass=*)',attrlist=None,attrsonly=0,serverctrls=None,clientctrls=None,timeout=-1,sizelimit=0,criticality=True,page_size=1000):
"""
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
"""
req_ctrl = SimplePagedResultsControl(criticality,size=page_size,cookie='')
# Send first search request
msgid = self.search_ext(
base,
scope,
filterstr,
attrlist=attrlist,
serverctrls=(serverctrls or [])+[req_ctrl]
)
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,
attrlist=attrlist,
serverctrls=(serverctrls or [])+[req_ctrl]
)
else:
break
return result_pages,all_results
示例14: search
# 需要导入模块: from ldap.controls import SimplePagedResultsControl [as 别名]
# 或者: from ldap.controls.SimplePagedResultsControl import cookie [as 别名]
def search(self, base_dn=None, scope=ldap.SCOPE_SUBTREE,
filter='(objectClass=*)', attrs=[]):
conn = self.connect()
if conn is None:
return []
if base_dn is None:
base_dn = self.base_dn
# 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,
filter,
attrs,
serverctrls=[lc])
rtype, rdata, rmsgid, serverctrls = conn.result3(msgid)
results.extend(rdata)
pctrls = [c for c in serverctrls
if c.controlType == LDAP_CONTROL_PAGED_RESULTS]
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[1] = cookie
else:
is_last_page = True
else:
is_last_page = True
logger.warn("Server ignores paged results control (RFC 2696).")
return results
示例15: _search
# 需要导入模块: from ldap.controls import SimplePagedResultsControl [as 别名]
# 或者: from ldap.controls.SimplePagedResultsControl import cookie [as 别名]
def _search(self, search_flt, attrs, limit=None, path_prefix=None):
"""
Query this academic calendar for records matching the search filter.
Only looks one level deep in the tree to improve performance, so make
sure you pass the dn that you want to look directly inside of as
the `path` parameter.
Returns a list of records.
Each record is a dictionary containing the requested attributes.
search_flt -- LDAP-style search filter
attrs -- attributes to be retrieved
limit -- max number of records to return
path -- extra LDAP dn: is prepended to this object's basedn
ie the LDAP directory to look from relative to the root
"""
if limit is None:
limit = float('inf')
if path_prefix is None:
path = self._basedn
else:
path = '{}{}'.format(path_prefix, self._basedn)
page_control = SimplePagedResultsControl(
criticality=True, size=RemoteLDAPDatabase.PAGE_SIZE, cookie=''
)
results = []
pages_retrieved = 0
first_pass = True
while pages_retrieved*RemoteLDAPDatabase.PAGE_SIZE < limit \
and page_control.cookie \
or first_pass:
first_pass = False
try:
msgid = self._client.search_ext(path, ldap.SCOPE_ONELEVEL,
search_flt, attrlist=attrs,
serverctrls=[page_control])
except:
raise
pages_retrieved += 1
_, result_data, msgid, serverctrls = self._client.result3(msgid)
page_control.cookie = serverctrls[0].cookie
results += extract_results_from_ldap_data(result_data)
return results