Python ldap實現(xiàn)登錄實例代碼
更新時間:2016年09月30日 13:59:26 作者:張瑜
今天給大家分享python idap實現(xiàn)登錄的實例代碼,代碼簡單易懂,需要的朋友一起看看吧
下面一段代碼是小編給大家介紹的Python ldap實現(xiàn)登錄實例代碼,一起看看吧
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, #'用戶名或密碼錯誤' 2: 2, #ldap驗證異常' } 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 # 驗證用戶登錄 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 # 將上一步計算的id在下面運算 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-11pandas創(chuàng)建DataFrame的方式小結(jié)
今天給大家整理了pandas創(chuàng)建DataFrame的方式小結(jié),現(xiàn)在我們就來看看這三種生成Dataframe的方式,每種方式通過實例代碼給大家介紹的非常詳細,需要的朋友參考下吧2021-09-09