|
| 1 | +# LDAP Pooling example |
| 2 | + |
| 3 | +## entries as dict |
| 4 | + |
| 5 | +```python |
| 6 | +from ldappool import ConnectionPool |
| 7 | +pool = ConnectionPool( |
| 8 | + params={"keep": True, "autoBind": True, "retries": 2}, |
| 9 | + max=5) |
| 10 | +pool.set_uri("ldaps://ldap.example.com:636/dc=example,dc=com?uid,mail?sub?(|(uid=test)(mail=test@example.com))") |
| 11 | +pool.set_credentials("binddn", "bindpw") |
| 12 | +with pool.get() as conn: |
| 13 | + for entry in conn.search_s(pool.basedn, |
| 14 | + pool.scope, |
| 15 | + pool.filter, |
| 16 | + pool.attributes): |
| 17 | + print(f"{entry[0]}: {entry[1].get('uid')} {entry[1].get('mail')}") |
| 18 | + for member in entry[1].get("memberOf", []): |
| 19 | + print(member) |
| 20 | +``` |
| 21 | + |
| 22 | +## entry to dataclass example |
| 23 | +```python |
| 24 | +from ldappool import ConnectionPool |
| 25 | +from ldappool import e2c |
| 26 | +pool = ConnectionPool( |
| 27 | + params={"keep": True, "autoBind": True, "retries": 2}, |
| 28 | + max=5) |
| 29 | +pool.set_uri("ldaps://ldap.example.com:636/dc=example,dc=com?uid,mail?sub?(|(uid=test)(mail=test@example.com))") |
| 30 | +pool.set_credentials("binddn", "bindpw") |
| 31 | +with pool.get() as conn: |
| 32 | + for entry in map(e2c, conn.search_s(pool.basedn, |
| 33 | + pool.scope, |
| 34 | + pool.filter, |
| 35 | + pool.attributes)): |
| 36 | + print(f"{entry.dn}: {entry.uid} {entry.mail}") |
| 37 | + for member in entry.memberOf: |
| 38 | + print(member) |
| 39 | +``` |
| 40 | + |
| 41 | +## changing the connection or credentials for the pool |
| 42 | + |
| 43 | +```python |
| 44 | +from ldappool import ConnectionPool |
| 45 | +from ldappool import e2c |
| 46 | +pool = ConnectionPool( |
| 47 | + params={"keep": True, "autoBind": True, "retries": 2}, |
| 48 | + max=5) |
| 49 | +pool.set_uri("ldaps://ldap.example.com:636/dc=example,dc=com?uid,mail?sub?(|(uid=test)(mail=test@example.com))") |
| 50 | +pool.set_credentials("binddn", "bindpw") |
| 51 | +with pool.get() as conn: |
| 52 | + for entry in map(e2c, conn.search_s(pool.basedn, |
| 53 | + pool.scope, |
| 54 | + pool.filter, |
| 55 | + pool.attributes)): |
| 56 | + print(f"{entry.dn}: {entry.uid} {entry.mail}") |
| 57 | + |
| 58 | +pool.set_credentials(entry.dn, "changeme") |
| 59 | +with pool.get() as conn: |
| 60 | + for entry in map(e2c, conn.search_s(pool.basedn, |
| 61 | + pool.scope, |
| 62 | + pool.filter, |
| 63 | + pool.attributes)): |
| 64 | + print(f"{entry.dn}: {entry.uid} {entry.mail}") |
| 65 | +``` |
0 commit comments