1Ldap_config = {2 'Ldap_path':'ldap://xx.xx.xx.xx:389',3 'base_dn':'ou=users,dc=ledo,dc=com',4 'Ldap_user':'uid=reporttest,ou=users,dc=ledo,dc=com',5 'Ldap_pass':'111111.0',6 'Original_pass':'111111.0'7 }8 9Ldap_message = {Ten0:0,#' OK ' One1:1,#' User name or password error ' A2:2,#LDAP Authentication exception ' - } - the ImportLDAP - ImportBase64 - ImportHashlib - fromConfig_messageImportLdap_config, Ldap_message + - + classLdap_api (object): A at_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'] - in #connecting to an LDAP server - def __init__(self): to + Try: -Self.ldapconn =ldap.initialize (Self._ldap_path) theSelf.ldapconn.protocal_version =LDAP. VERSION3 * Self.ldapconn.simple_bind (Self._ldap_user, Self._ldap_pass) $ Panax Notoginseng exceptLDAP. Ldaperror, E: - Printe the + #Verifying user logon A defLdap_check_login (self, username, password): the +obj =Self.ldapconn -SearchScope =LDAP. Scope_subtree $ #searchfilter = ' (& (cn= ' +username+ ') (userpassword= ' +password+ ')) ' $Searchfilter ='uid='+username - - Try: theObj.search (SELF._BASE_DN, SearchScope, Searchfilter, None)#id--2 - #The ID calculated in the previous step is evaluated belowWuyiResult_type, Result_data = Obj.result (2, 0) the ifResult_type! =LDAP. Res_search_entry: - return{'Status': Ldap_message[1],'Data':"'} WuDIC = result_data[0][1] -L_realname = dic['SN'][0] AboutL_password = dic['UserPassword'][0] $Md_password =ldap_api.hash_md5 (password) - ifL_passwordinch(password, md_password): - return{'Status': Ldap_message[0],'Data': L_realname} - Else: A return{'Status': Ldap_message[1],'Data':"'} + exceptLDAP. Ldaperror, E: the return{'Status': Ldap_message[2],'Data':"'} - $ @staticmethod the defhash_md5 (data): theMD =hashlib.md5 () the md.update (str (data)) theA =md.digest () -b ='{MD5}'+Base64.b64encode (a) in returnB
Link http://blog.csdn.net/shanliangliuxing/article/details/8266267
PYTHON-LDAP Implementing a login case