Python ldap3 重新绑定方法不会引发错误

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)

In case the credentials are invalid or if the server doesn’t allow you to rebind the server could abruptly close the connection. This condition is checked by the rebind() method and an LDAPBindError exception will be raised if caugh. Link to this

问题是虽然一切看起来都很好,但我可以通过打印 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()。它 return 如果绑定成功则为真,如果不成功则为假。如果您想在凭据无效时引发异常,则必须在 Connection() 定义中使用 raise_exceptions=True 参数。

仅当服务器在尝试再次绑定时关闭连接时才会引发 LdapBindError 异常。请记住,网络错误总是引发异常,即使 raise_exceptions 设置为 False。

会尽快更新文档(我是 ldap3 的作者)。