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爬取網(wǎng)站數(shù)據(jù)保存使用的方法
這篇文章主要介紹了使用Python從網(wǎng)上爬取特定屬性數(shù)據(jù)保存的方法,其中解決了編碼問題和如何使用正則匹配數(shù)據(jù)的方法,詳情看下文2013-11-11python 根據(jù)時(shí)間來生成唯一的字符串方法
今天小編就為大家分享一篇python 根據(jù)時(shí)間來生成唯一的字符串方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01Python?多線程知識(shí)點(diǎn)總結(jié)及實(shí)例用法
在本篇內(nèi)容里小編給大家整理了一篇關(guān)于Python?多線程知識(shí)點(diǎn)總結(jié)及實(shí)例用法,對(duì)想好學(xué)習(xí)PY的用戶非常友好,需要的參考下吧。2021-12-12pandas創(chuàng)建DataFrame的方式小結(jié)
今天給大家整理了pandas創(chuàng)建DataFrame的方式小結(jié),現(xiàn)在我們就來看看這三種生成Dataframe的方式,每種方式通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-09-09python識(shí)別圖像并提取文字的實(shí)現(xiàn)方法
這篇文章主要介紹了python識(shí)別圖像并提取文字的實(shí)現(xiàn)方法,2019-06-06