在我本机电脑实现。
Python2.7.8 Windows7 64bit
"""test ldap connection by using ldap3"""from ldap3 import Server, Connectiondef 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 "", line 1, in 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>>>
问题还未解决。