python实例26[验证用户是否存在于LDAP Server]
来源:互联网 发布:进销存软件永久免费版 编辑:程序博客网 时间:2024/06/06 02:49
需要安装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
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
完!
- python实例26[验证用户是否存在于LDAP Server]
- SQL Server 存储过程-验证用户是否存在
- Ajaxa验证用户是否存在
- ajax验证用户是否存在
- 动态验证用户是否存在
- Ajax验证用户是否存在
- 【python】判断key是否存在于dict
- 无刷新验证用户是否存在
- 无刷新页面验证用户是否存在
- Ajax实现异步验证用户是否存在
- struts2+jquery验证注册用户是否存在
- 使用Ajax验证用户是否已存在
- LDAP用户验证(Spring-LDAP)
- LDAP用户验证(Spring-LDAP)
- LDAP用户验证(Spring-LDAP)
- 迈瑞ldap用户验证
- Ranger用户同步于LDAP
- Asp.net 2.0 Ajax 验证用户是否存在
- python总结
- Windows上使用Linux shell
- Linux的cron和crontab
- LDAP基础
- 正则表达式测试工具
- python实例26[验证用户是否存在于LDAP Server]
- ftp的主动模式(port)与被动模式(PASV)
- VMWare网络设置的3中方式
- 第三次C语言上机指导
- ~~~Linux面试题汇总答案~~
- GDB调试器用法
- checkinstall制作linux安装包
- 查看系统的所有port的状态
- python技巧26[python的egg包的安装和制作]