Python ldap實(shí)現(xiàn)登錄實(shí)例代碼
更新時(shí)間:2016年09月30日 13:59:26 作者:張瑜
今天給大家分享python idap實(shí)現(xiàn)登錄的實(shí)例代碼,代碼簡(jiǎn)單易懂,需要的朋友一起看看吧
下面一段代碼是小編給大家介紹的Python ldap實(shí)現(xiàn)登錄實(shí)例代碼,一起看看吧
ldap_config = {
'ldap_path': 'ldap://xx.xx.xx.xx:389',
'base_dn': 'ou=users,dc=ledo,dc=com',
'ldap_user': 'uid=reporttest,ou=users,dc=ledo,dc=com',
'ldap_pass': '111111.0',
'original_pass': '111111.0'
}
ldap_message = {
0: 0, #'ok'
1: 1, #'用戶名或密碼錯(cuò)誤'
2: 2, #ldap驗(yàn)證異常'
}
import ldap
import base64
import hashlib
from config_message import ldap_config, ldap_message
class LDAP_API(object):
_ldap_path = ldap_config['ldap_path']
_base_dn = ldap_config['base_dn']
_ldap_user = ldap_config['ldap_user']
_ldap_pass = ldap_config['ldap_pass']
_original_pass = ldap_config['original_pass']
# 連接ldap服務(wù)器
def __init__(self):
try:
self.ldapconn = ldap.initialize(self._ldap_path)
self.ldapconn.protocal_version = ldap.VERSION3
self.ldapconn.simple_bind(self._ldap_user, self._ldap_pass)
except ldap.LDAPError, e:
print e
# 驗(yàn)證用戶登錄
def ldap_check_login(self, username, password):
obj = self.ldapconn
searchScope = ldap.SCOPE_SUBTREE
# searchFilter = '(&(cn='+username+')(userPassword='+password+'))'
searchFilter = 'uid=' + username
try:
obj.search(self._base_dn, searchScope, searchFilter, None) # id--2
# 將上一步計(jì)算的id在下面運(yùn)算
result_type, result_data = obj.result(2, 0)
if result_type != ldap.RES_SEARCH_ENTRY:
return {'status': ldap_message[1], 'data': ''}
dic = result_data[0][1]
l_realname = dic['sn'][0]
l_password = dic['userPassword'][0]
md_password = LDAP_API.hash_md5(password)
if l_password in (password, md_password):
return {'status': ldap_message[0], 'data': l_realname}
else:
return {'status': ldap_message[1], 'data': ''}
except ldap.LDAPError, e:
return {'status': ldap_message[2], 'data': ''}
@staticmethod
def hash_md5(data):
md = hashlib.md5()
md.update(str(data))
a = md.digest()
b = '{MD5}' + base64.b64encode(a)
return b
您可能感興趣的文章:
相關(guān)文章
python paramiko模塊學(xué)習(xí)分享
這篇文章主要為大家分享了python paramiko模塊的學(xué)習(xí)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
python?Sweetviz探索性數(shù)據(jù)可視化分析庫(kù)使用特征詳解
這篇文章主要為大家介紹了python?Sweetviz探索性數(shù)據(jù)可視化分析庫(kù)特征使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
django中的自定義分頁(yè)器的實(shí)現(xiàn)示例
本文主要介紹了django中的自定義分頁(yè)器的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08

