1、安装python-ldap
https://pypi.python.org/pypi/python-ldap/
在 Ubuntu/Debian 下安装 python-ldap 模块: $ sudo apt-get install python-ldap 在 CentOS/RHEL 下安装 python-ldap 模块: # yum install python-ldap
2、执行代码
import ldap conn = ldap.initialize('ldap://host') conn.simple_bind_s('domainusername', 'password')
方法二
# -*- coding: utf-8 -*-
import web
import ldap
'''
<!--域认证地址-->
<add key="DomainUriCount" value="2"/>
<add key="DomainUri1" value="LDAP://rdev.k.net/DC=rdev,DC=k,DC=net"/>
<add key="DomainUri2" value="LDAP://k.cn/DC=k,DC=cn"/>
'''
def login(name, pwd):
if name == '' or pwd == '':
return 0
if validateLDAPUser2(name, pwd):
return 1
else:
return 0
'''
if validateLDAPUser(name, pwd) or validateLDAPUser2(name, pwd):
return 1
else:
return 0
'''
def validateLDAPUser(user, pwd):
bRet = False
try:
l = ldap.initialize('LDAP://rdev.k.net')
l.simple_bind_s('rdev\'+user, pwd)
bRet = True
except Exception as err:
print err
return bRet
def validateLDAPUser2(user, pwd):
bRet = False
try:
print("validateLDAPUser2")
l = ldap.initialize('LDAP://k.cn')
# print user,pwd
l.simple_bind_s('k\'+user, pwd)
bRet = True
print bRet
except Exception as err:
print err
return bRet