Warm tip: This article is reproduced from serverfault.com, please click

python-ldap3打印假列表和空列表。

(python - ldap3 print false and empty list. Why is the list empty?)

发布于 2020-12-01 09:12:44

我现在设法建立了一个连接,但是现在只有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
[]
Questioner
Ina Heike
Viewed
0
LisaJ 2020-12-02 12:08:54

导入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)