代码:
def ldap_add_user(self, mail , username , password , mobile , displayname):
result = None
try:
# if self.is_english_char(cn) is False:
obj = self.ldapconn
obj.protocal_version = ldap.VERSION3
addDN = "uid=%s,ou=Company,%s" % (username, BASE_DN)
attrs = {}
attrs['objectclass'] = ['top' , 'person' , 'inetOrgPerson' , 'posixAccount' , 'organizationalPerson' , 'shadowAccount']
attrs['cn'] = username.encode()
attrs['homeDirectory'] = '/home/%s' % username
attrs['mail'] = mail
attrs['sn'] = username
attrs['uid'] = username
attrs['userPassword'] = password
attrs['uidNumber'] = str(self.get_max_uidNumber())
attrs['gidNumber'] = self.ldap_getgid(cn='common').decode(encoding='utf-8')
attrs['displayname'] = displayname
attrs['mobile'] = mobile
attrs['l'] = 'common'
attrs['o'] = 'common'
#密码到2018年12月过期
attrs['shadowexpire'] = '17621'
attrs['shadowlastchange'] = '0'
attrs['shadowmax'] = '90'
attrs['shadowwarning'] = '10'
ldif = ldap.modlist.addModlist(attrs)
obj.add_s(addDN, ldif)
obj.unbind_s()
result = True
except ldap.LDAPError as e:
logger.error("生成用户%s 失败,原因为: %s" % (username, str(e)))
return result
#测试添加
ldap_add_user('lisi@qq.com','lisi','lisi','18710092000','李四')
报错:
TypeError: ('expected a byte string in the list', 'c')
问题 :list已经是string 为啥报错。