Usage from PythonΒΆ

These samples assume that you have a LDAPConnector instance set up already with ID conn.

Adding a server definition:

>>> conn.addServer('localhost', '1389', 'ldap')

To work with the connection object you need to make sure that a LDAP server is available on the provided host and port.

Now we will search for a record that does not yet exist, then add the missing record and find it when searching again:

 >>> conn.search('ou=users,dc=localhost', fltr='(cn=testing)')
 {'exception': '', 'results': [], 'size': 0}
 >>> data = { 'objectClass': ['top', 'inetOrgPerson']
 ...        , 'cn': 'testing'
 ...        , 'sn': 'Lastname'
 ...        , 'givenName': 'Firstname'
 ...        , 'mail': 'test@test.com'
 ...        , 'userPassword': '5ecret'
 ...        }
 >>> conn.insert('ou=users,dc=localhost', 'cn=testing', attrs=data, bind_dn='cn=Manager,dc=localhost', bind_pwd='secret')
 >>> conn.search('ou=users,dc=localhost', fltr='(cn=testing)')
 {'exception': '', 'results': [{'dn': 'cn=testing,ou=users,dc=localhost', 'cn': ['testing'], 'objectClass': ['top', 'inetOrgPerson'], 'userPassword': ['5ecret'], 'sn': ['Lastname'], 'mail': ['test@test.com'], 'givenName': ['Firstname']}], 'size': 1}

We can edit an existing record:

1
2
3
4
>>> changes = {'givenName': 'John', 'sn': 'Doe'}
>>> conn.modify('cn=testing,ou=users,dc=localhost', attrs=changes, bind_dn='cn=Manager,dc=localhost', bind_pwd='secret')
>>> conn.search('ou=users,dc=localhost', fltr='(cn=testing)')
{'exception': '', 'results': [{'dn': 'cn=testing,ou=users,dc=localhost', 'cn': ['testing'], 'objectClass': ['top', 'inetOrgPerson'], 'userPassword': ['5ecret'], 'sn': ['Doe'], 'mail': ['test@test.com'], 'givenName': ['John']}], 'size': 1}

As the last step, we will delete our testing record:

1
2
3
>>> conn.delete('cn=testing,ou=users,dc=localhost', bind_dn='cn=Manager,dc=localhost', bind_pwd='secret')
>>> conn.search('ou=users,dc=localhost', fltr='(cn=testing)')
{'exception': '', 'results': [], 'size': 0}

The Interfaces page contains more information about the connection APIs.

Table Of Contents

Previous topic

Next topic

Related links

This Page


Download as PDF