[英]Python ldap3 rebind method doesn't raise error
我有以下代碼
from ldap3 import Server, Connection, SUBTREE, ALL_ATTRIBUTES, LDAPBindError
...
...
def search(self, id):
if not self._connect.bind():
print('error in bind', self._connect.result)
else:
self._connect.search(
search_base=self._base_dn,
search_filter='(uid='+id+')',
search_scope=SUBTREE
)
userdn = self._connect.response[0]['dn']
try:
self._connect.rebind(user=userdn, password='password')
print(self._connect.result)
except LDAPBindError:
print('error in rebind', self._connect.result)
self._connect.unbind()
pass
根據python-ldap3
文檔, rebind
方法應引發LDAPBindError
文件:
# import class and constants
from ldap3 import Server, Connection, ALL, LDAPBindError
# define the server
s = Server('servername', get_info=ALL) # define an unsecure LDAP server, requesting info on DSE and schema
# define the connection
c = Connection(s, user='user_dn', password='user_password')
# perform the Bind operation
if not c.bind():
print('error in bind', c.result)
try:
c.rebind(user='different_user_dn', password='different_user_password')
except LDAPBindError:
print('error in rebind', c.result)
如果憑據無效或服務器不允許您重新綁定,則服務器可能會突然關閉連接。 此條件由rebind()方法檢查,並且如果caugh會引發LDAPBindError異常。 連結到這個
問題是,盡管一切似乎都正常,但我可以通過打印result
屬性來驗證這一點。
成功重新綁定時: {'result': 0, 'description': 'success', 'type': 'bindResponse', 'message': '', 'dn': '', 'referrals': None, 'saslCreds': None}
重新綁定失敗時: {'type': 'bindResponse', 'dn': '', 'result': 49, 'description': 'invalidCredentials', 'message': '', 'referrals': None, 'saslCreds': None}
盡管重新綁定失敗,但不會引發異常。 我是否理解錯了並且不應該提出錯誤? 否則為什么不這樣做,我錯了嗎?
謝謝你的幫助。
文檔已過時。 rebind()方法的行為類似於bind()。 如果綁定成功,則返回True,否則返回false。 如果您希望在憑據無效時引發異常,則必須在Connection()定義中使用raise_exceptions = True參數。
僅當服務器在嘗試再次綁定時關閉連接時,才會引發LdapBindError異常。 請記住,即使將raise_exceptions設置為False,網絡錯誤也總是引發異常。
即將更新文檔(我是ldap3的作者)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.