python实例26[验证用户是否存在于LDAP Server]

 

需要安装python2.x 和python-LDAP模块。

python-ldap:http://www.python-ldap.org/

python-ldap的windows版本下载:http://pypi.python.org/pypi/python-ldap/

 

python26实例代码:(用来验证某用户是否存在于LDAP Server)

import  time
import  ldap

'''
    Need install python-ldap module from:
      http://www.python-ldap.org/
    For windows OS, you can get the module from:
      http://pypi.python.org/pypi/python-ldap/
'''

ldapuser 
=   " yourusername " ;
# ldapuser = "CN=yourusername,OU=XXX,OU=XXX,DC=XXX,DC=XXXXX,DC=com"
ldappass  =   " youruserpasswd " ;
ldappath 
=   " ldap://yourldapserveriporname:yourldapserverport/ " ;

baseDN 
=   " DC=XXX,DC=XXXXX,DC=COM "

FoundResult_ServerBusy 
=   " Server is busy "
FoundResult_NotFound 
=   " Not Found "
FoundResult_Found 
=   " Found "


def  _validateLDAPUser(user):
    
try :
        l 
=  ldap.initialize(ldappath)
        l.protocol_version 
=  ldap.VERSION3
        l.simple_bind(ldapuser,ldappass)

        searchScope  
=  ldap.SCOPE_SUBTREE
        searchFiltername 
=   " sAMAccountName "
        retrieveAttributes 
=  None
        searchFilter 
=   ' ( '   +  searchFiltername  +   " = "   +  user  + ' ) '

        ldap_result_id 
=  l.search(baseDN, searchScope, searchFilter, retrieveAttributes)
        result_type, result_data 
=  l.result(ldap_result_id, 1 )
        
if ( not  len(result_data)  ==  0):
          
# print result_data
           return   1 , FoundResult_Found
        
else :
          
return  0, FoundResult_NotFound
    
except  ldap.LDAPError, e:
        
# print e
         return  0, FoundResult_ServerBusy
    
finally :
        l.unbind()
        
del  l

def  validateLDAPUser(user, trynum  =   30 ):
    i 
=  0
    isfound 
=  0
    foundResult 
=   ""
    
while (i  <  trynum):
        
# print "try: " + str(i)
        isfound, foundResult  =  _validateLDAPUser(user)
        
if (isfound):
          
break
        
# time.sleep(60)
        i += 1
    
print   " ------------------------------- "
    
print   " user is : "   +  user
    
print   " isfound : "   +  str(isfound)
    
print   " FoundResult :  "   +  foundResult
    
return  isfound, foundResult

 

 

 

参考:

http://www.grotan.com/ldap/python-ldap-samples.html

http://www.linuxforum.net/forum/gshowflat.php?Cat=&Board=python&Number=533078&page=1&view=collapsed&sb=5&o=all

 

http://www.python-ldap.org/doc/html/index.html
 

 

 

完!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值