ldap3 打印错误和空列表。为什么列表是空的?
ldap3 print false and empty list. Why is the list empty?
我现在成功建立了连接,但是现在只有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)
我现在成功建立了连接,但是现在只有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)