我正在使用 ldapsearch 在 python 中编写代码,并在rosettacode.org中遇到了这个链接
import ldap
l = ldap.initialize("ldap://ldap.example.com")
try:
l.protocol_version = ldap.VERSION3
l.set_option(ldap.OPT_REFERRALS, 0)
bind = l.simple_bind_s("me@example.com", "password")
base = "dc=example, dc=com"
criteria = "(&(objectClass=user)(sAMAccountName=username))"
attributes = ['displayName', 'company']
result = l.search_s(base, ldap.SCOPE_SUBTREE, criteria, attributes)
results = [entry for dn, entry in result if isinstance(entry, dict)]
print results
finally:
l.unbind()
我在这里无法理解这段代码results = [entry for dn, entry in result if isinstance(entry, dict)]
- 我在上面的代码中没有看到
dn
定义,那么它来自哪里? - 做什么
isinstance(entry, dict)
?
当我尝试执行它时,我可以看到它返回了所有 ldap 条目的列表,以及与之关联的相应属性。首字母result
也返回一个列表。有人可以解释一下results
代码的作用吗?