简体   繁体   中英

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:

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

But how do I do it if I am using Reader?

So after reading the source code, there is an undocumented function inside Reader

    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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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