擅长:python、mysql、java
<p>我更喜欢使用<code>ldap3</code>而不是<code>ldap</code>。
<a href="https://ldap3.readthedocs.io/" rel="nofollow noreferrer">https://ldap3.readthedocs.io/</a></p>
<p>这是因为sAMAccountName是条目的一个属性</p>
<p>你能试试这个代码吗</p>
<pre><code>from ldap3 import Server, Connection, SUBTREE, ALL_ATTRIBUTES
import json
server = Server("ldap://url:port")
connection = Connection(server, "CN=ABC,OU=XYZ,OU=AAA,DC=users,DC=net", "password")
connection.search(
"DC=users,DC=net",
"(objectCategory=user)",
search_scope=SUBTREE,
attributes=ALL_ATTRIBUTES,
)
data = json.loads(connection.response_to_json())
entries = data["entries"]
for entry in entries:
print(entry["attributes"])
print(entry["attributes"]["sAMAccountName"])
</code></pre>