Python-ldap login case, python-ldap case

Source: Internet
Author: User

Python-ldap login case, python-ldap case

1 ldap_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': '2017. 0', 6' original _ pass': '2017. 0 '7} 8 9 ldap_message = {10 0: 0, # 'OK' 11 1: 1, # 'incorrect username or password '12 2: 2, # ldap verification exception '13} 14 15 import ldap16 import base6417 import hashlib18 from config_message import ldap_config, ldap_message19 20 21 class LDAP_API (object ): 22 23 _ ldap_path = ldap_config ['ldap _ path'] 24 _ base_dn = ldap_config ['base _ dn'] 25 _ ldap_user = ldap_config ['ldap _ user'] 26 _ ldap_pass = ldap_config ['ldap _ pass'] 27 _ original_pass = ldap_config ['original _ pass'] 28 29 # connect to the ldap server 30 def _ init _ (self): 31 32 try: 33 self. ldapconn = ldap. initialize (self. _ ldap_path) 34 self. ldapconn. protocal_version = ldap. VERSION335 self. ldapco Nn. simple_bind (self. _ ldap_user, self. _ ldap_pass) 36 37 TB ldap. LDAPError, e: 38 print e39 40 # verify User Logon 41 def ldap_check_login (self, username, password): 42 43 obj = self. ldapconn44 searchScope = ldap. SCOPE_SUBTREE45 # searchFilter = '(& (cn =' + username + ') (userPassword =' + password + ') '46 searchFilter = 'uid =' + username47 48 try: 49 obj. search (self. _ base_dn, searchScope, searchFilter, None) # id -- 250 # Calculate the id calculated in the previous step 51 result_type, result_data = obj. result (2, 0) 52 if result_type! = Ldap. RES_SEARCH_ENTRY: 53 return {'status': ldap_message [1], 'data ': ''} 54 dic = result_data [0] [1] 55 l_realname = dic ['sn '] [0] 56 l_password = dic ['userpassword'] [0] 57 md_password = LDAP_API.hash_md5 (password) 58 if l_password in (password, md_password): 59 return {'status': ldap_message [0], 'data': l_realname} 60 else: 61 return {'status ': ldap_message [1], 'data': ''} 62 TB ldap. LDAPError, e: 63 return {'status': ldap_message [2], 'data': ''} 64 65 @ staticmethod66 def hash_md5 (data): 67 md = hashlib. md5 () 68 md. update (str (data) 69 a = md. digest () 70 B = '{MD5}' + base64.b64encode (a) 71 return B

Link http://blog.csdn.net/shanliangliuxing/article/details/8266267

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.