Python ldap实现登录实例代码
2016-09-30 00:00
1026 查看
下面一段代码是小编给大家介绍的Python ldap实现登录实例代码,一起看看吧
Python的Flask框架应用程序实现使用QQ账号登录的方法
Python爬虫模拟登录带验证码网站
Python模拟百度登录实例详解
python实现网站的模拟登录
Python实现模拟登录及表单提交的方法
python下paramiko模块实现ssh连接登录Linux服务器
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
您可能感兴趣的文章:
使用Python的Flask框架表单插件Flask-WTF实现Web登录验证Python的Flask框架应用程序实现使用QQ账号登录的方法
Python爬虫模拟登录带验证码网站
Python模拟百度登录实例详解
python实现网站的模拟登录
Python实现模拟登录及表单提交的方法
python下paramiko模块实现ssh连接登录Linux服务器
相关文章推荐
- Python ldap实现登录实例代码
- Python 实现12306登录功能实例代码
- python双向链表实现实例代码
- 使用python实现baidu hi自动登录的代码
- 基于Python实现的扫雷游戏实例代码
- PHP实现QQ登录实例代码
- Python实现高效求解素数代码实例
- python cookielib 登录人人网的实现代码
- Python实现多线程下载文件的代码实例
- 基于Python实现的扫雷游戏实例代码
- python实现的udp协议Server和Client代码实例
- Python实现端口复用实例代码
- ssh批量登录并执行命令的python实现代码
- python实现自动登录人人网并访问最近来访者实例
- Python使用Socket(Https)Post登录百度的实现代码
- ssh批量登录并执行命令的python实现代码
- ssh批量登录并执行命令的python实现代码
- js实现简单登录功能的实例代码
- Python实现高效求解素数代码实例
- python实现自动登录人人网并访问最近来访者实例