The following code is small to introduce you to the Python LDAP implementation login instance code, look at it together
Ldap_config = {' Ldap_path ': ' ldap://xx.xx.xx.xx:389 ', ' base_dn ': ' ou=users,dc=ledo,dc=com ', ' ldap_user ': ' Uid=repo
Rttest,ou=users,dc=ledo,dc=com ', ' ldap_pass ': ' 111111.0 ', ' original_pass ': ' 111111.0 '} ldap_message = {0:0, # ' OK ' 1:1, # ' username or password error ' 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 '] # Connect LDAP server Def __init__ (self): Try:self.ldapconn = Ldap.initialize (Self._ldap_path) Self.ldapconn.prot Ocal_version = LDAP. VERSION3 Self.ldapconn.simple_bind (Self._ldap_user, self._ldap_pass) except LDAP. Ldaperror, e:print e # Verify user login def ldap_check_login (self, username, password): obj = Self.ldapconn searc Hscope = LDAP. Scope_subtree # sEarchfilter = ' (& (cn= ' +username+ ') (userpassword= ' +password+ ')) ' Searchfilter = ' uid= ' + username Try:ob J.search (SELF._BASE_DN, SearchScope, Searchfilter, None) # id--2 # The ID computed in the previous step is calculated below result_type, result_data = O Bj.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 {' s Tatus ': 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