我现在设法建立了一个连接,但是现在只有False和一个空列表出来了。我削减了search_base。这是我第一次与ldap合作。
ldap.py:
import ldap3
from ldap3 import Server, Connection, ALL
s = 'ldap://...int'
user = 'Y'
password = '1234'
server = Server(s, get_info = ALL)
conn = Connection(server,user,password)
print(conn.bind())
result = conn.search(search_base='ou=...,dc=...,dc=...,dc=...,', search_filter='(initials=user)')
print(result)
print(conn.entries)
输出:
False
False
[]
导入LDAPBindError并将绑定尝试包装在try / except中。然后,如果你的连接或绑定尝试不起作用,你应该得到一些指示(例如,如果目录服务器主机名错误,则为“ ldap3.core.exceptions.LDAPSocketOpenError:无效的服务器地址”)。
import ldap3
from ldap3 import Server, Connection, ALL
from ldap3.core.exceptions import LDAPBindError
server = Server('ldap://directoryserver.example.com', get_info = ALL)
con = Connection(server,'MyUser@example.com','P2s%w0rd')
try:
con.bind()
except LDAPBindError:
print(con.result)
谢谢你。他现在建立了连接,因为我发现我的用户错了。