Python uses LDAP instance __python

Source: Internet
Author: User
Tags base64 ldap
#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 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.