如果已经问过这个问题,请提前道歉,已经做了一些搜索但没有运气。
TL;DR:试图将元组的第二部分作为字符串拉出。
我正在使用 python3 构建一个脚本,该脚本从 LDAP 中提取信息并返回希望是可操作的数据。
def GetMembersWDept():
i2 = input('Please input the department number. Examples: 2410, 2417\n')
criteria = '(&(objectClass=User)(department=' + i2 + '*))'
print ('Search criteria set to: \t\t' + criteria)
searchDC = 'dc=example,dc=com'
print ('Searching location: \t\t\t' + searchDC)
print ()
out = []
result = c.search(searchDC, criteria, \
SEARCH_SCOPE_WHOLE_SUBTREE, attributes = ['department']) \
# request a few object from the ldap server
if result:
for r in c.response:
if (r['type'] == 'searchResEntry'):
out.append(r['dn'])
out.append(r['attributes']) # comes out in tuple form
else:
pass
else:
print('result', conn.result)
return out
这适用于提取该部门成员的 CN,但不适用于提取任何附加信息,在本例中是附加的部门。
示例输出为:
搜索条件设置为:(&(objectClass=User)(department=2417*)) 搜索位置:dc=ple,dc=com
['CN=First Last,OU=ex,OU=am,DC=ple,DC=com', {'department': ['1234']}, 'CN=Another Person,OU=ex,OU=am, DC=ple,DC=com', {'部门': ['1234']}]
如何将元组的第二部分(即“1234”)作为字符串提取出来?这里的最后阶段是将数据格式化为:
[第一个最后,1234,另一个人,1234]
...所以我可以在另一个比较部门并在不满足条件时返回名称的函数中使用它。