首页 > 解决方案 > 如何使用 Python 连接到 LDAP 服务器(版本 3.8.8)

问题描述

我需要使用 Python(版本 3.8.8)连接到 LDAP 服务器,并且我已经尝试复制一些示例,例如下面链接中显示的示例,但它们都不适合我。我要么得到 (TypeError: iter() returned non-iterator of type 'NoneType') 要么 (PyAsn1Error: Attempted "__iter__" operation on ASN.1 schema object)。示例中使用的 ldap3 和 python-ldap 似乎没有更新为与 python 3.8.8 一起使用。如果有人能给我一个连接 LPDAP 服务器或帮助我解决这个问题的另一个现有库的真实示例,我将非常高兴。

(1) https://sixfeetup.com/blog/new-ldap3-python-ldap-library
(2) https://ldap3.readthedocs.io/en/latest/tutorial_intro.html
(3) https://stackoverflow.com/questions/58907026/ldap3-bind-failed-when-cn-and-displayname-are-different

我对链接 3 中示例的测试错误:

from ldap3 import Server, Connection
server = Server('ipa.demo1.freeipa.org')
conn = Connection(server)
conn.bind()

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
ipython-input-4-470896b147da> in <module>
      2 server = Server('ipa.demo1.freeipa.org')
      3 conn = Connection(server)
----> 4 conn.bind()

C:\ProgramData\Anaconda3\lib\site-packages\ldap3\core\connection.py in bind(self, read_server_info, controls)
    418                     if log_enabled(PROTOCOL):
    419                         log(PROTOCOL, 'anonymous BIND request <%s> sent via <%s>', bind_request_to_dict(request), self)
--> 420                     response = self.post_send_single_response(self.send('bindRequest', request, controls))
    421                 elif self.authentication == SIMPLE:
    422                     if log_enabled(PROTOCOL):

C:\ProgramData\Anaconda3\lib\site-packages\ldap3\strategy\sync.py in post_send_single_response(self, message_id)
    120         Returns the result message or None
    121         """
--> 122         responses, result = self.get_response(message_id)
    123         self.connection.result = result
    124         if result['type'] == 'intermediateResponse':  # checks that all responses are intermediates (there should be only one)

C:\ProgramData\Anaconda3\lib\site-packages\ldap3\strategy\base.py in get_response(self, message_id, timeout)
    296         if self._outstanding and message_id in self._outstanding:
    297             while timeout >= 0:  # waiting for completed message to appear in responses
--> 298                 responses = self._get_response(message_id)
    299                 if not responses:
    300                     sleep(RESPONSE_SLEEPTIME)

C:\ProgramData\Anaconda3\lib\site-packages\ldap3\strategy\sync.py in _get_response(self, message_id)
    166                             log(EXTENDED, 'ldap message received via <%s>:%s', self.connection, format_ldap_message(ldap_resp, '<<'))
    167                         if int(ldap_resp['messageID']) == message_id:
--> 168                             dict_response = self.decode_response(ldap_resp)
    169                             ldap_responses.append(dict_response)
    170                             if dict_response['type'] not in ['searchResEntry', 'searchResRef', 'intermediateResponse']:

C:\ProgramData\Anaconda3\lib\site-packages\ldap3\strategy\base.py in decode_response(self, ldap_message)
    401         if message_type == 'bindResponse':
    402             if not bytes(component['matchedDN']).startswith(b'NTLM'):  # patch for microsoft ntlm authentication
--> 403                 result = bind_response_to_dict(component)
    404             else:
    405                 result = sicily_bind_response_to_dict(component)

C:\ProgramData\Anaconda3\lib\site-packages\ldap3\operation\bind.py in bind_response_to_dict(response)
    116             'dn': str(response['matchedDN']),
    117             'message': str(response['diagnosticMessage']),
--> 118             'referrals': referrals_to_list(response['referral']),
    119             'saslCreds': bytes(response['serverSaslCreds']) if response['serverSaslCreds'] is not None else None}
    120 

C:\ProgramData\Anaconda3\lib\site-packages\ldap3\protocol\convert.py in referrals_to_list(referrals)
     42 
     43 def referrals_to_list(referrals):
---> 44     return [str(referral) for referral in referrals if referral] if referrals else None
     45 
     46 

C:\ProgramData\Anaconda3\lib\site-packages\pyasn1\type\base.py in __bool__(self)
    572     else:
    573         def __bool__(self):
--> 574             return bool(self.components)
    575 
    576     @property

C:\ProgramData\Anaconda3\lib\site-packages\pyasn1\type\univ.py in components(self)
   1958     def components(self):
   1959         return [self._componentValues[idx]
-> 1960                 for idx in sorted(self._componentValues)]
   1961 
   1962     def clear(self):
TypeError: iter() returned non-iterator of type 'NoneType'

标签: pythonldap

解决方案


这是库版本的问题......我运行了“pip install ldap3 upgrade”命令,它工作正常


推荐阅读