您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

138 行
5.2 KiB

  1. """
  2. """
  3. # Created on 2016.07.08
  4. #
  5. # Author: Giovanni Cannata
  6. #
  7. # Copyright 2016 - 2018 Giovanni Cannata
  8. #
  9. # This file is part of ldap3.
  10. #
  11. # ldap3 is free software: you can redistribute it and/or modify
  12. # it under the terms of the GNU Lesser General Public License as published
  13. # by the Free Software Foundation, either version 3 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # ldap3 is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU Lesser General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU Lesser General Public License
  22. # along with ldap3 in the COPYING and COPYING.LESSER files.
  23. # If not, see <http://www.gnu.org/licenses/>.
  24. try:
  25. from queue import Empty
  26. except ImportError: # Python 2
  27. # noinspection PyUnresolvedReferences
  28. from Queue import Empty
  29. from ...core.exceptions import LDAPExtensionError
  30. from ...protocol.persistentSearch import persistent_search_control
  31. from ... import SEQUENCE_TYPES
  32. from ...utils.dn import safe_dn
  33. class PersistentSearch(object):
  34. def __init__(self,
  35. connection,
  36. search_base,
  37. search_filter,
  38. search_scope,
  39. dereference_aliases,
  40. attributes,
  41. size_limit,
  42. time_limit,
  43. controls,
  44. changes_only,
  45. events_type,
  46. notifications,
  47. streaming,
  48. callback
  49. ):
  50. if connection.strategy.sync:
  51. raise LDAPExtensionError('Persistent Search needs an asynchronous streaming connection')
  52. if connection.check_names and search_base:
  53. search_base = safe_dn(search_base)
  54. self.connection = connection
  55. self.changes_only = changes_only
  56. self.notifications = notifications
  57. self.message_id = None
  58. self.base = search_base
  59. self.filter = search_filter
  60. self.scope = search_scope
  61. self.dereference_aliases = dereference_aliases
  62. self.attributes = attributes
  63. self.size_limit = size_limit
  64. self.time_limit = time_limit
  65. self.connection.strategy.streaming = streaming
  66. if callback and callable(callback):
  67. self.connection.strategy.callback = callback
  68. elif callback:
  69. raise LDAPExtensionError('callback is not callable')
  70. if not isinstance(controls, SEQUENCE_TYPES):
  71. self.controls = []
  72. else:
  73. self.controls = controls
  74. if events_type and changes_only and notifications:
  75. self.controls.append(persistent_search_control(events_type, changes_only, notifications))
  76. self.start()
  77. def start(self):
  78. if self.message_id: # persistent search already started
  79. return
  80. if not self.connection.bound:
  81. self.connection.bind()
  82. with self.connection.strategy.async_lock:
  83. self.message_id = self.connection.search(search_base=self.base,
  84. search_filter=self.filter,
  85. search_scope=self.scope,
  86. dereference_aliases=self.dereference_aliases,
  87. attributes=self.attributes,
  88. size_limit=self.size_limit,
  89. time_limit=self.time_limit,
  90. controls=self.controls)
  91. self.connection.strategy.persistent_search_message_id = self.message_id
  92. def stop(self, unbind=True):
  93. self.connection.abandon(self.message_id)
  94. if unbind:
  95. self.connection.unbind()
  96. if self.message_id in self.connection.strategy._responses:
  97. del self.connection.strategy._responses[self.message_id]
  98. if hasattr(self.connection.strategy, '_requests') and self.message_id in self.connection.strategy._requests: # asynchronous strategy has a dict of request that could be returned by get_response()
  99. del self.connection.strategy._requests[self.message_id]
  100. self.connection.strategy.persistent_search_message_id = None
  101. self.message_id = None
  102. def next(self, block=False, timeout=None):
  103. if not self.connection.strategy.streaming and not self.connection.strategy.callback:
  104. try:
  105. return self.connection.strategy.events.get(block, timeout)
  106. except Empty:
  107. return None
  108. raise LDAPExtensionError('Persistent search is not accumulating events in queue')
  109. def funnel(self, block=False, timeout=None):
  110. esci = False
  111. while not esci:
  112. try:
  113. entry = self.connection.strategy.events.get(block, timeout)
  114. except Empty:
  115. yield None
  116. if entry['type'] == 'searchResEntry':
  117. yield entry
  118. else:
  119. esci = True
  120. yield entry