ldap3打印错误和空列表。为什么列表是空的?

2024-05-15 11:00:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我现在已经成功地建立了一个连接,但现在只有False和一个空列表出现。我已经削减了搜索基数。这是我第一次使用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
[]

Tags: importfalsesearchserverpasswordresultallconnection
1条回答
网友
1楼 · 发布于 2024-05-15 11:00:12

导入LDAPBindError并将绑定尝试包装为try/except。然后,如果您的连接或绑定尝试不起作用,您应该得到一些指示(例如,如果目录服务器主机名不正确,“ldap3.core.exceptions.LDAPSocketOpenError:invalid server address”)

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)

相关问题 更多 >