01. LDAP sample

It requires ldap3 and python-ldap-test [2017!].

Warning

Not working!

11 # Generic
12 import ldap3
13
14 # Specific
15 from ldap_test import LdapServer
16
17 # Create LDAP server.
18 server = LdapServer({
19     'port': 3333,
20     'bind_dn': 'cn=admin,dc=zoldar,dc=net',
21     'password': 'pass1',
22     'base': {'objectclass': ['domain'],
23              'dn': 'dc=zoldar,dc=net',
24              'attributes': {'dc': 'zoldar'}},
25     'entries': [
26         {'objectclass': 'domain',
27          'dn': 'dc=users,dc=zoldar,dc=net',
28          'attributes': {'dc': 'users'}},
29         {'objectclass': 'organization',
30          'dn': 'o=foocompany,dc=users,dc=zoldar,dc=net',
31          'attributes': {'o': 'foocompany'}},
32     ]
33 })
34
35 try:
36     server.start()
37
38     dn = "cn=admin,dc=zoldar,dc=net"
39     pw = "pass1"
40
41     srv = ldap3.Server('localhost', port=3333)
42     conn = ldap3.Connection(srv, user=dn, password=pw, auto_bind=True)
43
44     base_dn = 'dc=zoldar,dc=net'
45     search_filter = '(objectclass=organization)'
46     attrs = ['o']
47
48     conn.search(base_dn, search_filter, attributes=attrs)
49
50     print(conn.response)
51     # [{
52     #    'dn': 'o=foocompany,dc=users,dc=zoldar,dc=net',
53     #    'raw_attributes': {'o': [b'foocompany']},
54     #    'attributes': {'o': ['foocompany']},
55     #    'type': 'searchResEntry'
56     # }]
57 finally:
58     server.stop()

Total running time of the script: ( 0 minutes 0.000 seconds)

Gallery generated by Sphinx-Gallery