Python3 ldap密钥错误:';属性';
在加载Python3 ldap密钥错误:';属性';,python,python-3.x,ldap,Python,Python 3.x,Ldap,在加载python3ldap模块的情况下使用Python3.4。使用代码: from ldap3 import Server, Connection, SEARCH_SCOPE_WHOLE_SUBTREE, AUTO_BIND_NO_TLS #For title queires into LDAP def GetTitle(u): print(u) t=[] server = Server(DomanCtrlr) c = Connection(server,
python3ldap
模块的情况下使用Python3.4。使用代码:
from ldap3 import Server, Connection, SEARCH_SCOPE_WHOLE_SUBTREE, AUTO_BIND_NO_TLS #For title queires into LDAP
def GetTitle(u):
print(u)
t=[]
server = Server(DomanCtrlr)
c = Connection(server,
auto_bind=AUTO_BIND_NO_TLS,
read_only=True,
check_names=True,
user = user,
password= password)
c.search(search_base = 'dc=corp,dc=weyer,dc=pri',
search_filter = '(&(samAccountName=' + u + '))',
search_scope = SEARCH_SCOPE_WHOLE_SUBTREE,
attributes = ['title'],
paged_size = 5)
for entry in c.response:
print(entry['attributes']['title'])
t = entry['attributes']['title']
print(u, " : ", t)
users = ['user1', 'notAuser', 'user2']
for u in users:
GetTitle(u)
我希望为notAuser获得一个错误,但是我得到了以下输出:
user1
['CONTROL ROOM OPERATOR']
user1 : ['CONTROL ROOM OPERATOR']
Traceback (most recent call last):
File "C:\Users\olsonma\Documents\ThreatMatrix_PY\LDAPTest.py", line 28, in <module>
GetTitle(u)
File "C:\Users\olsonma\Documents\ThreatMatrix_PY\LDAPTest.py", line 17, in GetTitle
print(entry['attributes']['title'])
KeyError: 'attributes'
user1
[“控制室操作员”]
用户1:[“控制室操作员”]
回溯(最近一次呼叫最后一次):
文件“C:\Users\olsonma\Documents\ThreatMatrix\u PY\LDAPTest.PY”,第28行,在
盖蒂特尔(u)
文件“C:\Users\olsonma\Documents\ThreatMatrix\u PY\LDAPTest.PY”,第17行,在GetTitle中
打印(条目['attributes']['title'])
KeyError:“属性”
由于该行显然已执行,我不清楚错误是如何发生的
我已经找到了许多关于类似于此的旧的pythonldap
错误的文章,但是为pythonldap修复此错误的选项并不是python3ldap
的选项。有人知道吗
1)为什么会发生这种情况?
2.)如何使其停止?自从我发现问题后,已从代码中删除打印内容
from ldap3 import Server, Connection, SEARCH_SCOPE_WHOLE_SUBTREE, AUTO_BIND_NO_TLS #For title queires into LDAP
def GetTitle(u):
t=''
server = Server(DomainCtrlr)
c = Connection(server,
auto_bind=AUTO_BIND_NO_TLS,
read_only=True,
check_names=True,
user = user,
password= password)
c.search(search_base = 'dc=corp,dc=weyer,dc=pri',
search_filter = '(&(samAccountName=' + u + '))',
search_scope = SEARCH_SCOPE_WHOLE_SUBTREE,
attributes = ['title'],
paged_size = 5)
if len(c.response) > 1:
for entry in c.response:
t = entry['attributes']['title']
return t
users = ['lafrenh', 'userid', 'garlockb']
for u in users:
title = GetTitle(u)
print(title)
我刚刚意识到它正在尝试第二次运行for循环,所以我打印了完整的c.response,并在响应中找到了第二个不包含“attributes”关键字的条目。我相信我的解决办法是检查响应的长度,只有在有两个响应时才进行处理,然后只处理第一个响应项。如果有效,将发布固定代码。