2014-07-18 53 views
1

我試圖用python ldap添加條目。我得到一個命名約定的錯誤。我的代碼是添加項Python-LDAP

import ldap 
import ldap.modlist as modlist 

LOGIN = "" 
PASSWORD = '' 
LDAP_URL = "ldap://127.0.0.1:389" 
user='grant' 
l = ldap.initialize(LDAP_URL) 
l.bind(LOGIN, PASSWORD) 
dn="ou=Enki Users,dc=enki,dc=local" 

attrs = {} 
attrs['objectclass'] = ['top','organizationalRole','simpleSecurityObject'] 
attrs['cn'] = 'test' 
attrs['userPassword'] = 'test' 
attrs['description'] = 'User object for replication using slurpd' 

# 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() 

的錯誤是:

ldap.NAMING_VIOLATION: {'info': "00002099: NameErr: DSID-0305109C, problem 2005 (NAMING_VIOLATION), data 0, best match of:\n\t'dc=enki,dc=local'\n", 'desc': 'Naming violation'} 

運行,但不插入用戶到correc組織單位是下面的代碼。但是,即使它運行,我無法找到活動目錄中的用戶。請幫我看看什麼是錯的。我基本上爲用戶管理製作一個django webform。

import ldap 
import ldap.modlist as modlist 

LOGIN = "" 
PASSWORD = '' 
LDAP_URL = "ldap://127.0.0.1:389" 
user='grant' 
l = ldap.initialize(LDAP_URL) 
l.bind(LOGIN, PASSWORD) 

dn="cn=test,ou=Enki Users,dc=enki,dc=local" 

attrs = {} 
attrs['objectclass'] = ['top','organizationalRole','simpleSecurityObject'] 
attrs['cn'] = 'test' 
attrs['userPassword'] = 'test' 
attrs['description'] = 'User object for replication using slurpd' 

# 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() 

回答

0

我猜測(但沒有測試證明它),你的錯誤的根本原因是,您的項目不包含在你的條目的DN,最左邊的屬性相匹配的「命名屬性」,這在你的情況是ou = Enki用戶。要將此命名屬性添加到條目中,可以在填充attrs字典的代碼部分添加以下行。

attrs ['ou'] ='Enki Users'