Skip to main content
 首页 » 编程设计

python之ldap3 bind 无法将 'NoValue' 对象转换为字节

2025年01月19日10bluestorm

运行python3.6
ldap3-python库
在 docker 容器上使用 OpenLDAP osixia/docker-openldap
端口 389 和 636 已打开。我可以在 python 的控制台中看到连接;但是,我遇到了问题。
在主机上的 jupyter 笔记本上运行 python

我似乎无法让 conn.bind() 工作。这是我的代码:

from ldap3 import Connection, Server, ALL 
 server = Server('127.0.0.1') 
 conn = Connection(server, user='cn=admin,dc=example,dc=org',  
 password="admin") 
 conn.open() 
 conn.bind() 

这里是错误:

>-------------------------------------------------------------------------->- 
>TypeError                                 Traceback (most recent call last) 
><ipython-input-3-5c5644694a2b> in <module>() 
      2 conn = Connection(server, user='cn=admin,dc=example,dc=org', password="admin") 
      3 conn.open() 
----> 4 conn.bind() 
 
>~/anaconda3/lib/python3.6/site-packages/ldap3/core/connection.py in bind(self, read_server_info, controls) 
    425                     if log_enabled(PROTOCOL): 
    426                         log(PROTOCOL, 'simple BIND request <%s> sent via <%s>', bind_request_to_dict(request), self) 
--> 427                     response = self.post_send_single_response(self.send('bindRequest', request, controls)) 
    428                 elif self.authentication == SASL: 
    429                     if self.sasl_mechanism in SASL_AVAILABLE_MECHANISMS: 
 
>~/anaconda3/lib/python3.6/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) 
 
>~/anaconda3/lib/python3.6/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) 
 
>~/anaconda3/lib/python3.6/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']: 
 
>~/anaconda3/lib/python3.6/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) 
 
>~/anaconda3/lib/python3.6/site-packages/ldap3/operation/bind.py in bind_response_to_dict(response) 
    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  
    121  
 
>~/anaconda3/lib/python3.6/site-packages/pyasn1/type/univ.py in __bytes__(self) 
    888  
    889         def __bytes__(self): 
--> 890             return bytes(self._value) 
    891  
    892         def asOctets(self): 
 
>TypeError: cannot convert 'NoValue' object to bytes 

我看到连接发生在我的 ldap 服务器上:

ldap_1 | 5a794f6d conn=1045 fd=15 ACCEPT from IP=172.20.0.1:42060 (IP=0.0.0.0:389) ldap_1 | 5a794f6d conn=1045 op=0 BIND dn="cn=admin,dc=example,dc=org" method=128 ldap_1 | 5a794f6d conn=1045 op=0 BIND dn="cn=admin,dc=example,dc=org" mech=SIMPLE ssf=0
ldap_1 | 5a794f6d conn=1045 op=0 RESULT tag=97 err=0 text=

当我进行本地连接时,我会返回结果,这是我在服务器上看到的:

ldap_1 | 5a795085 conn=1046 fd=16 ACCEPT from IP=127.0.0.1:48488 (IP=0.0.0.0:389)
ldap_1 | 5a795085 conn=1046 op=0 BIND dn="cn=admin,dc=example,dc=org" method=128
ldap_1 | 5a795085 conn=1046 op=0 BIND dn="cn=admin,dc=example,dc=org" mech=SIMPLE ssf=0
ldap_1 | 5a795085 conn=1046 op=0 RESULT tag=97 err=0 text=
ldap_1 | 5a795085 conn=1046 op=1 SRCH base="dc=example,dc=org" scope=2 deref=0 filter="(objectClass=*)"
ldap_1 | 5a795085 conn=1046 op=1 SEARCH RESULT tag=101 err=0 nentries=8 text=
ldap_1 | 5a795085 conn=1046 op=2 UNBIND ldap_1 | 5a795085 conn=1046 fd=16 closed

我已经搜索了一段时间,但没有成功。我希望有人能帮助我 :D

编辑:仍在处理这个问题,但运气不佳:(

请您参考如下方法:

尝试更新最新的ldap3和pyasn1版本。