简体   繁体   English

Python ldap3 如何使用Reader进行分页

[英]Python ldap3 how to do pagination using Reader

See following code:请参见以下代码:

class SomeClass():

    self.person_cls = ldap3.ObjectDef(['user', 'person', 'organizationalPerson'], self.connection)

    def get_all_users(self):
        log.info('Fetching all users...')
        r = ldap3.Reader(self.connection, self.person_cls, self.root_folder)
        return r.search()

This is a very time consuming search to return all users...这是一个非常耗时的搜索来返回所有用户......

There is a pagination documention but not related to the Reader abstraction:有一个分页文档,但与 Reader 抽象无关:

https://ldap3.readthedocs.io/en/latest/searches.html#simple-paged-search https://ldap3.readthedocs.io/en/latest/searches.html#simple-paged-search

But how do I do it if I am using Reader?但是,如果我使用的是 Reader,我该怎么做呢?

So after reading the source code, there is an undocumented function inside Reader所以看了源码后,Reader里面有一个无证的function

    def search_paged(self, paged_size, paged_criticality=True, generator=True, attributes=None):
        """Perform a paged search, can be called as an Iterator

        :param attributes: optional attributes to search
        :param paged_size: number of entries returned in each search
        :type paged_size: int
        :param paged_criticality: specify if server must not execute the search if it is not capable of paging searches
        :type paged_criticality: bool
        :param generator: if True the paged searches are executed while generating the entries,
                          if False all the paged searches are execute before returning the generator
        :type generator: bool
        :return: Entries found in search

So tested with my code and works fine:所以用我的代码测试并且工作正常:

class SomeClass():

    self.person_cls = ldap3.ObjectDef(['user', 'person', 'organizationalPerson'], self.connection)

    def get_all_users(self):
        log.info('Fetching all users...')
        r = ldap3.Reader(self.connection, self.person_cls, self.root_folder)
        return r.search_paged(paged_size=50)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM