2016-11-28 62 views
0

我有以下代碼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}

雖然未能重新綁定引發任何異常。我有沒有理解錯誤,不應該提出錯誤?否則爲什麼它不,我有錯?

感謝您的任何幫助。

回答

2

文檔已過時。 rebind()方法的行爲與bind()相似。如果綁定成功則返回True,如果不成功則返回false。如果要在憑據無效時引發異常,則必須在Connection()定義中使用raise_exceptions = True參數。

僅當服務器在嘗試再次綁定時關閉連接時纔會引發LdapBindError異常。請記住,即使raise_exceptions設置爲False,網絡錯誤也會引發異常。

即將更新文檔(我是ldap3的作者)。