Python ldap实现登录实例代码

作者:张瑜 时间:2021-03-12 21:38:55 

下面一段代码是小编给大家介绍的Python ldap实现登录实例代码,一起看看吧


ldap_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 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']
 # 连接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

来源:http://www.cnblogs.com/work115/archive/2016/09/30/5923278.html

标签:python,ldap,登录
0
投稿

猜你喜欢

  • SQL Server错误代码大全及解释(留着备用)

    2012-07-11 16:17:03
  • MySQL组提交group commit详解

    2024-01-21 05:33:09
  • vue 使用微信jssdk,调用微信相册上传图片功能

    2024-05-02 17:09:36
  • python3下载抖音视频的完整代码

    2023-12-24 01:10:42
  • Go语言中的range用法实例分析

    2024-04-30 10:03:18
  • Pandas数据分析固定时间点和时间差

    2022-03-05 20:24:07
  • 深入浅析python的第三方库pandas

    2021-06-05 03:13:03
  • css分页放大镜效果

    2008-11-02 15:35:00
  • python程序中调用其他程序的实现

    2021-08-07 12:33:16
  • python执行系统命令后获取返回值的几种方式集合

    2022-07-24 22:43:56
  • ini_set的用法介绍

    2023-11-15 07:31:56
  • C#命名空间System.ComponentModel属性方法汇总

    2024-06-05 09:24:02
  • oracle数据库下统计专营店的男女数量的语句

    2024-01-17 04:18:17
  • 在Pytorch中计算卷积方法的区别详解(conv2d的区别)

    2021-02-12 01:01:03
  • Python数据结构之链表详解

    2023-07-21 04:16:19
  • SQL 外链接操作小结 inner join left join right join

    2024-01-19 21:06:39
  • python 美化输出信息的实例

    2022-04-15 09:53:54
  • 一篇文章搞懂Go语言中的Context

    2024-04-26 17:16:05
  • matplotlib中legend位置调整解析

    2023-07-17 03:00:44
  • Ubuntu中MySQL的参数文件my.cnf示例详析

    2024-01-13 02:26:28
  • asp之家 网络编程 m.aspxhome.com