Python LDAP Implementation Login Instance code _python

Source: Internet
Author: User
Tags base64 ldap md5

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

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.