使用python ldap添加组

使用python ldap添加组,ldap,openldap,Ldap,Openldap,我需要添加几个成员的组。所以我使用下面的代码 import ldap import ldap.modlist as modlist # Open a connection l = ldap.initialize("ldap://localhost:389/") # Bind/authenticate with a user with apropriate rights to add objects l.simple_bind_s("cn=manager,dc=maxcrc,dc=com","

我需要添加几个成员的组。所以我使用下面的代码

import ldap
import ldap.modlist as modlist

# Open a connection
l = ldap.initialize("ldap://localhost:389/")

# Bind/authenticate with a user with apropriate rights to add objects
l.simple_bind_s("cn=manager,dc=maxcrc,dc=com","secret")

# The dn of our new entry/object
dn="cn=semuaFK,ou=group,dc=maxcrc,dc=com" 

# A dict to help build the "body" of the object
attrs = {}
attrs['objectclass'] = ['top','groupofnames']
attrs['member'] = 'cn=user1,ou=people,dc=maxcrc,dc=com'
attrs['member'] = 'cn=user2,ou=people,dc=maxcrc,dc=com'
attrs['description'] = 'ini group untuk semua dosen dokter'

# Convert our dict to nice syntax for the add-function using modlist-module
ldif = modlist.addModlist(attrs)

# Do the actual synchronous add-operation to the ldapserver
l.add_s(dn,ldif)

# Its nice to the server to disconnect and free resources when done
l.unbind_s()
将成员添加到组中

attrs['member'] = 'cn=user1,ou=people,dc=maxcrc,dc=com'
attrs['member'] = 'cn=user2,ou=people,dc=maxcrc,dc=com'

但结果是一个只有一名成员的小组。只有user2被添加到组中。如何创建一个组,还要向其中添加几个成员

成员
是一个多值属性(就像
对象类
)。查看代码时,问题似乎是您用第2行覆盖了
成员
属性的值。代码应该是这样的:

attrs['member'] = [ 'cn=users1,ou=people,dc=maxcrc,dc=com', 'cn=users2,ou=people,dc=maxcrc,dc=com' ]`

attrs['member'] = [ 'cn=users1,ou=people,dc=maxcrc,dc=com' ]
attrs['member'] += 'cn=users2,ou=people,dc=maxcrc,dc=com'