How to do it...

First we will read LDAP with a Reader object as shown by Listing 11.8 as follows:

#!/usr/bin/env python 
# Python Network Programming Cookbook, Second Edition
-- Chapter - 11 # This program is optimized for Python 2.7.12 and
Python 3.5.2. # It may run on any other version with/without
modifications. # Adopted from http://ldap3.readthedocs.io/tutorial_abstraction_basic.html from ldap3 import Server, Connection, ObjectDef, AttrDef, Reader, Writer, ALL def main(): server = Server('ipa.demo1.freeipa.org', get_info=ALL) conn = Connection(server, 'uid=admin,cn=users,cn=accounts,dc=demo1,dc=freeipa,dc=org', 'Secret123', auto_bind=True) person = ObjectDef('person', conn) r = Reader(conn, person, 'ou=ldap3-
tutorial,dc=demo1,dc=freeipa,dc=org') print(r) print('************') person+='uid' print(r) if __name__ == '__main__': main ()

This recipe performs an implicit creation of a new attribute definition by the following line:

    person+='uid' 

By running this recipe, you may observe the following output:

$ python 11_8_read_ldap_server.py 
CURSOR : Reader
CONN   : ldap://ipa.demo1.freeipa.org:389 - cleartext - user: uid=admin,cn=users,cn=accounts,dc=demo1,dc=freeipa,dc=org - not lazy - bound - open - <local: 192.168.137.95:44860 - remote: 52.57.162.88:389> - tls not started - listening - SyncStrategy - internal decoder
DEFS   : [u'person'] [cn, description, objectClass, seeAlso, sn, telephoneNumber, userPassword]
ATTRS  : [u'cn', u'description', u'objectClass', u'seeAlso', u'sn', u'telephoneNumber', u'userPassword']
BASE   : 'ou=ldap3-tutorial,dc=demo1,dc=freeipa,dc=org' [SUB]
FILTER : u'(objectClass=person)'
    
************
CURSOR : Reader
CONN   : ldap://ipa.demo1.freeipa.org:389 - cleartext - user: uid=admin,cn=users,cn=accounts,dc=demo1,dc=freeipa,dc=org - not lazy - bound - open - <local: 192.168.137.95:44860 - remote: 52.57.162.88:389> - tls not started - listening - SyncStrategy - internal decoder
DEFS   : [u'person'] [cn, description, objectClass, seeAlso, sn, telephoneNumber, uid, userPassword]
ATTRS  : [u'cn', u'description', u'objectClass', u'seeAlso', u'sn', u'telephoneNumber', u'userPassword']
BASE   : 'ou=ldap3-tutorial,dc=demo1,dc=freeipa,dc=org' [SUB]
FILTER : u'(objectClass=person)'

As highlighted in the preceding recipe, after the attribute definition uid is added to the person object, it is reflected in the DEFS after the line: person+='uid'. However, if you re-execute the recipe, you will notice that the previous changes to the person object are not present. This is because the changes are not written with the Reader cursor. For that, you will need a Writer. You may initiate a Writer from the Reader cursor as shown here:

    w = Writer.from_cursor(r) 
    w[0].sn += 'Smyth' 
    w.commit() 

Make sure you have the write access to the LDAP server with the correct dn and password for the commit to succeed. It is recommended to try this in a private LDAP server that gives you complete admin access.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.138.104