python 使用ldap实例
来源:互联网 发布:软件开发的文档 编辑:程序博客网 时间:2024/05/16 10:21
#coding: utf-8ldap_config = { 'ldap_path': 'ldap://xx.xx.xx.xx:389', 'base_dn': 'ou=users,dc=ledo,dc=com', 'ldap_user': 'uid=reporttest,ou=users,dc=ledo,dc=com', 'ldap_pass': '111111.0', 'original_pass': '111111.0'}ldap_message = { 0: 0, #'ok' 1: 1, #'用户名或密码错误' 2: 2, #ldap验证异常'}import ldapimport base64import hashlibfrom config_message import ldap_config, ldap_messageclass 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'] # 连接ldap服务器 def __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) except ldap.LDAPError, e: print e # 验证用户登录 def ldap_check_login(self, username, password): obj = self.ldapconn searchScope = ldap.SCOPE_SUBTREE # searchFilter = '(&(cn='+username+')(userPassword='+password+'))' searchFilter = 'uid=' + username try: obj.search(self._base_dn, searchScope, searchFilter, None) # id--2 # 将上一步计算的id在下面运算 result_type, result_data = obj.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 {'status': 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
0 0
- python 使用ldap实例
- python+ldap实例
- python-ldap 实例
- 使用 Python-LDAP 操作 LDAP
- python实例26[验证用户是否存在于LDAP Server]
- ldap使用
- LDAP使用
- ldap使用
- LDAP实例设计
- LDAP Search 实例代码
- Python 和 LDAP
- python 访问LDAP服务器
- 通过python-ldap处理ldap服务器
- python ihooks使用实例
- python使用socket实例
- 一个Python使用实例
- Python format 使用实例
- Java实现ldap实例化
- SPI协议及其工作原理浅析
- JavaScript——Tab选项卡
- 工程管理
- 模拟实现 栈
- Non-Rigid Registration Under Isometric Deformations
- python 使用ldap实例
- js关系运算符不能连续使用and why?
- j2se(四)_数组,时间日期
- Java数组和链表的底层实现
- 队列的创建于基本操作
- NNU_20161020_PAT5-3 逆序的三位数
- 链表.u12657置换
- Linux下修改pip源提高下载速度
- 20 款前端开发文档和指南(1)