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

问题描述:

我有以下代码Python的LDAP3重新绑定方法不会引发错误

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) 

如果证书无效或者服务器不允许您重新绑定证书rver可能会突然关闭连接。这个条件是由rebind()方法检查的,如果是caugh,将会引发一个LDAPBindError异常。 Link to this

的问题是,虽然一切似乎运作良好,我可以确认,从打印result财产。

在succeful重新绑定: {'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的作者)。