PythonLDAP的包装。
ldappr的Python项目详细描述
ldappr是python ldap的一个包装器,用于快速轻松地处理与ldap兼容的存储库相关的常见管理任务。它在小的一次性脚本中特别有用,可以在ipython shell中完成任务,也可以交互完成任务。
安装
当然,python ldap应该已经安装好了。
pip install ldappr
连接
importldappr# authenticated bindldap=ldappr.connect_to('127.0.0.1','uid=admin,ou=system','secret')
检索对象
当你有一个连接,你可以搜索它。首先,指定 西奇基地。
ldap.search_base='ou=users,ou=system'
然后,获取一个或多个要操作的对象。
# retrieve a single objectuser=ldap.get('cn=jdoe')# retrieve a list of objectsusers=ldap.search('objectClass=inetOrgPerson')
做事
一旦你得到了一个物体,你就可以很容易地操纵它。所有更改都将立即反映在LDAP存储库中。
# pretty print the retrieved userprint(user)# get an attribute valuesn=user.attrs['sn']# set an attribute value (existing value will be removed)user.set_value('givenName','Jack')# add a value to a multi-valued attributeuser.add_value('mobile','0123456789')user.add_value('mobile','9876543210')# remove a value from a multi-valued attributeuser.remove_value('mobile','9876543210')
其他示例
# anonymous bindldap=ldappr.connect_to('127.0.0.1')# authenticated bind with more optionsldap=ldappr.connect_to('127.0.0.1','uid=admin,ou=system','secret',protocol='ldaps',port='10636',verify=False,search_base='ou=users,ou=system')# delete all objects with employeeType managerfordninldap.get_dn('employeeType=manager'):ldap.delete(dn)# set an attribute value for a known dnldap.set_value('cn=jdoe,ou=users,ou=system','givenName','Jack')# make an LDIF export for all userswithopen('export.ldif','a')asfile:foruserinldap.search('objectClass=inetOrgPerson'):file.write(user.to_ldif())