{"library":"ldap3","code":"import os\nfrom ldap3 import Server, Connection, SYNC, ANONYMOUS, SUBTREE\n\n# Configuration from environment variables for security and flexibility\nLDAP_SERVER_URI = os.environ.get('LDAP_SERVER_URI', 'ldap://localhost:389')\nLDAP_BIND_DN = os.environ.get('LDAP_BIND_DN', 'cn=admin,dc=example,dc=com')\nLDAP_BIND_PASSWORD = os.environ.get('LDAP_BIND_PASSWORD', 'adminpassword')\nLDAP_SEARCH_BASE = os.environ.get('LDAP_SEARCH_BASE', 'dc=example,dc=com')\nLDAP_SEARCH_FILTER = os.environ.get('LDAP_SEARCH_FILTER', '(objectClass=person)')\nLDAP_SEARCH_ATTRIBUTES = os.environ.get('LDAP_SEARCH_ATTRIBUTES', 'cn,mail').split(',')\n\ndef ldap_connect_and_search():\n    try:\n        # Define the LDAP server\n        s = Server(LDAP_SERVER_URI)\n\n        # Establish a connection. auto_bind=True performs the bind operation immediately.\n        # authentication=ANONYMOUS can be used if no credentials are required.\n        # For authenticated bind:\n        # c = Connection(s, user=LDAP_BIND_DN, password=LDAP_BIND_PASSWORD, client_strategy=SYNC, auto_bind=True)\n        c = Connection(s, user=LDAP_BIND_DN, password=LDAP_BIND_PASSWORD, client_strategy=SYNC, auto_bind=True, raise_exceptions=True)\n\n        print(f\"Connection status: {c.bound}\")\n\n        # Perform a search operation\n        # search_base: The base DN for the search\n        # search_filter: The LDAP filter string\n        # search_scope: The scope of the search (e.g., SUBTREE, BASE, LEVEL)\n        # attributes: List of attributes to retrieve, or ALL_ATTRIBUTES, ALL_OPERATIONAL_ATTRIBUTES\n        c.search(LDAP_SEARCH_BASE, LDAP_SEARCH_FILTER, search_scope=SUBTREE, attributes=LDAP_SEARCH_ATTRIBUTES)\n\n        # Process the search results\n        print(f\"Found {len(c.entries)} entries:\")\n        for entry in c.entries:\n            print(f\"  DN: {entry.entry_dn}\")\n            for attr in LDAP_SEARCH_ATTRIBUTES:\n                if hasattr(entry, attr):\n                    print(f\"    {attr}: {getattr(entry, attr).value}\")\n\n    except Exception as e:\n        print(f\"An LDAP error occurred: {e}\")\n    finally:\n        if 'c' in locals() and c.bound:\n            c.unbind()\n            print(\"Connection unbound.\")\n\nif __name__ == '__main__':\n    ldap_connect_and_search()","lang":"python","description":"This example demonstrates how to establish a connection to an LDAP server, perform a simple bind with credentials (or anonymously), and execute a search operation. It uses environment variables for sensitive configuration details. The `raise_exceptions=True` parameter is added to the `Connection` to ensure LDAP operation failures are surfaced as Python exceptions.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":0},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":0},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":0},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":0},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]}