ldap3 官方文档学习之增删改查操作

前言

公司部门培训用到 ldap3,布置了个作业,于是开始看官方文档学习中。我是直接从 LDAP Operations 部分开始看的。

主要就是官方文档提供了增删改查的接口,需要看懂函数和参数,然后就会用了。

增加操作

官方 add 函数
def add(self,
        dn,
        object_class=None,
        attributes=None,
        controls=None)

逐个参数解释:

  • dn:标识要添加的目标名字

  • object_class:要添加的标志类名称,可以是包含一个单一值或一串字符串

  • attributes:一个以 {‘attr1’: ‘val1’, ‘attr2’: ‘val2’, …} or {‘attr1’: [‘val1’, ‘val2’, …], …} 多值形式的字典

  • controls:发送请求额外的信息

举例
# import class and constants
from ldap3 import Server, Connection, ALL

# define the server
s = Server('servername', get_info=ALL)  # define an unsecure LDAP server, requesting info on DSE and schema

# define the connection
c = Connection(s, user='user_dn', password='user_password')

# perform the Add operation
c.add('cn=user1,ou=users,o=company', ['inetOrgPerson', 'posixGroup', 'top'], {
   'sn': 'user_sn', 'gidNumber': 0})
# equivalent to 等同上面
c.add('cn=user1,ou=users,o=company', attributes={
   'objectClass':  ['inetOrgPerson', 'posixGroup', 'top'], 'sn': 'user_sn', gidNumber: 0})

print(c.result)

# close the connection
c.unbind()

主要就是 add 函数传三个参数:dn、object_class、attributes。

  • dn 包含用户cn、ou、o等信息

删除操作

官方 delete 函数
def delete(self,
           dn,
           controls=None):

逐个参数解释:

  • dn:标识要删除的目标名字
  • controls:发送请求额外的信息
举例
from ldap3 import Server, Connection, ALL

# define the server
s = Server('servername', get_info=ALL)  # define an unsecure LDAP server, requesting info on DSE and schema

# define the connection
c = Connection(s, user='user_dn', password='user_password')

# perform the Delete operation
c.delete('cn=user1,ou=users,o=company')
print(c.result)

# close the connection
c.unbind()

主要就是 delete 函数传一个参数:dn。

  • dn 包含用户cn、ou、o等信息

修改操作

官方 modify 函数
def modify(self,
           dn,
           changes,
           controls=None):

逐个参数解释:

  • dn:标识要删除的目标名字
  • changes:一个要被展示在具体入口的修改的字典
  • controls:发送请求额外的信息
举例
# import class and constants
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wonz

创作不易,一块就行。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值