该说明基于python3和ldap3 2.5,只在Windows2012 R2的Active Directory上测试过
常见操作
1. 连接
server = Server(ldap_url, use_ssl=True, get_info='ALL')
conn = Connection(server, auto_bind=True, user=ldap_user,password=ldap_password, authentication='SIMPLE')
参数说明:
ldap_url:Ldap服务器地址,默认端口是389
auto_bind:指定在定义Connection对象时是否自动执行绑定
authentication:指定认证类型,可以为以下值ANONYMOUS,SIMPLE,SASL
2.查询
base_dn = 'OU=**'
attributes = ['sAMAccountName']
condition = '(sAMAccountName=test)'
conn.search(base_dn, condition, attributes=attributes)
condition为过滤条件,使用ldap搜索语法
3. 创建新纪录
dn = 'CN=test,OU=**'
attributes = {
"name":"test","description":"test"} # 该字典为一条记录必须的属性,根据ldap服务器的要求设置
obj