#coding: Utf-8 ldap_config = {' Ldap_path ': ' ldap://xx.xx.xx.xx:389 ', ' base_dn ': ' ou=users,dc=ledo,dc=com ', ' Ldap_u Ser ': ' uid=reporttest,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验证异常 '}ImportLdapImportBase64ImportHashlib fromConfig_messageImportLdap_config, Ldap_messageclassLdap_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 serverdef__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)exceptLdap. Ldaperror, E:PrintE # Verifying User LogindefLdap_check_login (self, username, password): obj = self.ldapconn searchscope = LDAP. Scope_subtree # searchfilter = ' (& (cn= ' +username+ ') (userpassword= ' +password+ ')) ' Searchfilter = ' uid= ' + usern AmeTry: Obj.search (SELF._BASE_DN, SearchScope, Searchfilter, None) # id--2 # Calculates the ID of the previous step in the following Operation Result_type, Resul T_data = Obj.result (2, 0)ifResult_type!= LDAP. Res_search_entry: return{' status ': ldap_message[1], ' data ': ' dic = result_data[0][1] L_realname = dic[' sn '][0] L_password = d ic[' UserPassword '][0] md_password = ldap_api.hash_md5 (password)ifL_password in(Password, Md_password): return{' status ': ldap_message[0], ' Data ': L_realname}Else: return{' status ': ldap_message[1], ' Data ': '}exceptLdap. Ldaperror, E: return{' status ': ldap_message[2], ' Data ': '} @staticmethoddefHASH_MD5 (data): MD = HASHLIB.MD5 () md.update (str (data)) A = Md.digest () b = ' {MD5} ' + Base64.b64encode (a) returnB