一、Ldap3 介绍

Ldap3 是一个基于 python 的 LDAP 库,它可以帮助开发者实现 LDAP 服务的访问、管理和开发。它是一个完全可靠的、可移植的、可扩展的和可配置的库。它的特点是可以支持 LDAP V3 的所有操作,无论是简单的查询还是复杂的搜索,Ldap3 都可以完成。

二、Ldap3 的安装

要使用 Ldap3,首先需要安装它。安装 Ldap3 的方法非常简单,只需要使用 pip 命令:

12pip install ldap3
Python

安装完成后,Ldap3 就可以使用了。

三、Ldap3 的使用

要使用 Ldap3,首先需要导入它:

12from ldap3 import Server, Connection
Python

然后,需要创建一个 Server 对象,并使用它来创建一个 Connection 对象:

1234server = Server('ldap.example.com', port=389, get_info=ALL)
conn = Connection(server, user='cn=admin,dc=example,dc=com', password='password')
conn.bind()
Python

最后,就可以使用 Connection 对象来执行 LDAP 操作了,比如搜索:

1234567conn.search('dc=example,dc=com', '(objectclass=person)', attributes=['cn', 'sn'])
for entry in conn.entries:
    print(entry.cn, entry.sn)
conn.unbind()
Python

以上就是 Ldap3 的使用方法。