使用包ldap3进行Python的LDAP操作3

本文介绍了一个使用Python ldap3库进行LDAP连接测试的例子。该例子分别针对两个不同的LDAP服务器进行了连接和用户信息检索操作,并展示了如何处理连接过程中的错误。

在我本机电脑实现。

Python2.7.8 Windows7 64bit



"""test ldap connection by using ldap3"""

from ldap3 import Server, Connection


def test_201_86():
    print("\ntest LDAP connection to 10.99.201.86")
    server = Server('10.99.201.86', port=10389)
    print('server = %s'%server)
    with Connection(server, user='uid=admin,ou=system', 
                    password='密码') as conn:
        conn.bind()
        print('conn bind')
        #print('server.info = %s'%server.info)
        conn.search('ou=users,ou=plmdeploy,dc=lenovo,dc=com', 
                    '(objectclass=person)', attributes=['uid', 'mail', 'cn'])
        if hasattr(conn, 'entries'):
            entries = conn.entries
            for entry in entries:
                print('entry: uid=%s, mail=%s'%(entry['uid'], entry['mail']))


def test_lenovo_com():
    print("\ntest LDAP connection to lenovo.com")
    server = Server('lenovo.com', port=3268)
    print('server = %s'%server)
    with Connection(server, 
                    user='CN=adplm,OU=Service Accounts,DC=lenovo,DC=com', 
                    password='密码') as conn:
        print('conn = %s'%conn)
        conn.bind()
        print('conn bind')
        #print('server.info = %s'%server.info)
        print('start to print all usres:')
        user_ids = ['zhoujj', 'heyan', 'mengqq', 'zhangymd']
        for user_id in user_ids:
            conn.search('CN=%s,OU=Beijing,OU=China,OU=User Accounts,DC=lenovo,DC=com'%user_id, 
                        '(objectclass=person)', 
                        attributes=['distinguishedName', 'mail', 'cn'])
            if hasattr(conn, 'entries') and conn.entries:
                entry = conn.entries[0]
                print('distinguishedName=%s, mail=%s, cn=%s'
                      %(entry['distinguishedName'], entry['mail'], entry['cn']))
            else:
                print('%s not found'%user_id)


if __name__ == '__main__':
    print("test ldap3")
    test_201_86()
    test_lenovo_com()
    print("\ntest end")

在linux下实现时报错:

>>> from ldap3 import Server,Connection
>>> conn = Connection(Server('10.99.201.86', port=10389), user='uid=admin,ou=system', password='密码')
>>> conn.bind()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/site-packages/ldap3/core/connection.py", line 530, in bind
    response = self.post_send_single_response(self.send('bindRequest', request, controls))
  File "/usr/lib/python2.6/site-packages/ldap3/strategy/sync.py", line 124, in post_send_single_response
    responses, result = self.get_response(message_id)
  File "/usr/lib/python2.6/site-packages/ldap3/strategy/base.py", line 342, in get_response
    raise LDAPSessionTerminatedByServerError(self.connection.last_error)
ldap3.core.exceptions.LDAPSessionTerminatedByServerError: session terminated by server
>>> 

问题还未解决。

转载于:https://my.oschina.net/shawnplaying/blog/1539867

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值