Skip to main content
 首页 » 编程设计

python之检索 ldap3 中所有属性的列表 (python3-ldap)

2024年10月17日7duanxz

服务器没有为 python-ldap 和 ldap3 库返回相同数量的属性。

缺少的属性是我必须执行某些操作的属性。

这是我用于 ldap3 的搜索查询示例:

from ldap3 import Server,Connection,ALL_ATTRIBUTES, ALL_OPERATIONAL_ATTRIBUTES,ALL,SEARCH_SCOPE_WHOLE_SUBTREE,SUBTREE 
 
host = something1 
user = something2 
password = something3 
baseDn = something4 
search_filter = "(uid=something5)" 
 
server = Server(host, get_info=ALL) 
conn = Connection(server,user, password,auto_bind=True,check_names=True) 
conn.search(baseDn,search_filter, search_scope=SEARCH_SCOPE_WHOLE_SUBTREE, attributes=['*']) 
 
entry = conn.entries 
print(json.loads(entry[0].entry_to_json())) 

与 python-ldap 一起使用的搜索查询:

searchScope = ldap.SCOPE_SUBTREE 
## retrieve all attributes  
retrieveAttributes = None 
ldap_result_id = l.search(baseDn, searchScope, searchFilter,   retrieveAttributes) 
result_set = [] 
while 1: 
    result_type, result_data = l.result(ldap_result_id, 0) 
    if (result_data == []): 
        break 
    else: 
        ## you could do whatever you want with the individual entry 
        ## The appending to list is just for illustration. 
        if result_type == ldap.RES_SEARCH_ENTRY: 
            result_set.append(result_data) 
 
print json.loads(result_set) 

如果有人可以发布,是否有任何方法可以检索 ldap3 中给定查询可用的所有属性。

请您参考如下方法:

如果你使用Reader类,你可以找到allowedAttributes和allowedAttributesEffective:

from ldap3 import Server,Connection,Reader,ObjectDef 
 
host = something1 
user = something2 
password = something3 
baseDn = something4 
search_filter = "(uid=something5)" 
 
server = Server(host, get_info=ALL) 
conn = Connection(server,user, password,auto_bind=True,check_names=True) 
inetorgperson = ObjectDef(['person','user'], conn) 
reader = Reader(conn,inetorgperson,baseDn,search_filter) 
 
reader.search() 
 
>>> len(reader[0].allowedAttributes) 
741 
>>> len(reader[0].allowedAttributesEffective) 
620